tofty VIP
Total posts: 54
11 Март 2018 02:16

Hello,

I would like to set the canonical line for articles via creating a field, so that I can set it myself, and it will then show up no matter how people come to the page, since they can get to a page different ways (via categories, via full list view, via search, via breadcrumb list, etc.). I know how to create and customize a field.

I am less concerned about the actual categories showing duplicate canonical data. I suspect it will be more difficult to set this up manually.

So, for now, I want to focus on how to override the insertion of the data that is in Cobalt for articles.

I have read through all the related threads in the forum regarding canonical. I am not a php coder, so I'm not fully sure on how everything is built, or how to track down all the files I'd need to customize.

I'd like it so it has an option, so that if the field that I create is populated, it would use it. If it is blank, it could use whatever the default set up is.

How would I go about doing this?

Thank you.

Thora

Последние изменения: 20 Апр 2018


Sergey
Total posts: 13,748
04 Апр 2018 11:21

You can simply create template for text field. Create output template abd use text field to enter URL. Inside field template use

$doc = JFactory::getDocument();
foreach($doc->_links as $lk => $dl)
{
    if($dl['relation'] == 'canonical')
    {
        unset($doc->_links[$lk]);
    }
}
$doc->addHeadLink($this->value, 'canonical');

Something like this.


tofty VIP
Total posts: 54
20 Апр 2018 22:50

Hello Sergey,

I was able to get this to work!

For anyone else looking to do this I created a new text field template output file, and removed this line:

<?php echo $this->value;?>

And replaced it with this code:

<?php
    $doc = JFactory::getDocument();
    foreach($doc->_links as $lk => $dl)
    {
        if($dl['relation'] == 'canonical')
        {
            unset($doc->_links[$lk]);
        }
    }
    $doc->addHeadLink($this->value, 'canonical');
?>

I also had to hide/delete the following coding in the file /public_html/components/com_cobalt/views/record/view.html.php (after I made a backup):

        foreach($this->document->_links as $lk => $dl)
        {
            if($dl['relation'] == 'canonical')
            {
                unset($this->document->_links[$lk]);
            }
        }

        $this->document->addHeadLink($this->item->canon, 'canonical');

It still will assign a canonical url if I don't put anything into the field.

I don't really know how this all works, but it does, and I'm thrilled!

Thank you very much!

Thora

Работает на Cobalt