contao / core

Contao 3 → see contao/contao for Contao 4
GNU Lesser General Public License v3.0
492 stars 213 forks source link

Add SEO Title to Articles #8417

Open nigelcopley opened 8 years ago

nigelcopley commented 8 years ago

This is useful when you have a page that contains multiple articles with read more as it gives you more flexibility and control over your SEO params. We've done it by extending the DCA (tl_article.php) and the Module (ModuleArticle.php) but would be useful for others as part of the core

leofeyer commented 8 years ago

How exactly would this work?

nigelcopley commented 8 years ago

Add a field into article for SEO Title image

This then overrides page title when article is rendered as page. I can share the code we used to extend the moduleArticle.php with you if you want.

leofeyer commented 8 years ago

What if there are multiple articles?

fritzmg commented 8 years ago

I think he means for the detail page of an article.

aschempp commented 8 years ago

I guess he means if you use /…/article/alias.html url where one article is rendered.

nigelcopley commented 8 years ago

As fritz and andreas say it's only applicable when the article is rendered

nigelcopley commented 8 years ago

dca/tl_article.php

<?php
/**
 * Extend default palette
 */
$GLOBALS['TL_DCA']['tl_article']['palettes']['default'] = str_replace
(
    'keywords',
    'keywords,seo_title',
    $GLOBALS['TL_DCA']['tl_article']['palettes']['default']
);

/**
 * Add fields
 */
$GLOBALS['TL_DCA']['tl_article']['fields']['seo_title'] = array
(
    'label'     => &$GLOBALS['TL_LANG']['tl_article']['seo_title'],
    'exclude'   => true,
    'inputType' => 'text',
    'eval'      => array('tl_class'=>'clr long'),
    'sql'       => "text NULL"
);

/modules/moduleArticle.php

<?php

namespace MyNameSpace;

class ModuleArticle extends \Contao\ModuleArticle
{
    /**
     * Generate module
     */
    protected function compile()
    {
        parent::compile();

        /** @var \PageModel $objPage */
        global $objPage;

        // Get section and article alias
        list($strSection, $strArticle) = explode(':', \Input::get('articles'));

        if ($strArticle === null)
        {
            $strArticle = $strSection;
        }

        if (!$this->blnNoMarkup && $strArticle != '' && ($strArticle == $this->id || $strArticle == $this->alias) && $this->title != '')
        {
            if ($this->seo_title) {
                $objPage->pageTitle = $this->seo_title;
            } else {
                $objPage->pageTitle = strip_tags(strip_insert_tags($this->title));
            }
        }
    }
}

langauges/en/tl_article.php

<?php
/**
 * Fields
 */
$GLOBALS['TL_LANG']['tl_article']['seo_title'] = array
(
    'SEO Title',
    'Here you can enter a custom title which will overwrite the page title'
);
leofeyer commented 8 years ago

Related tickets: #6582 #7830