1

I want to change the title in the index of an custom element.

public function getTableAttributeHtml(BaseElementModel $element, $attribute)
{
    switch ($attribute)
    {
        case 'title' :
        {
            return $element->attribute . 'blabla';
        }

        ...
    }
}

But whatever I try, the title field didn't change. Other fields are no problem. How can i do this?

5

I finally found the answer to this question.

From BaseElementType defineAvailableTableAttributes() docs,

The first item that this array returns will just identify the database column name, and the table column’s header, but will not have any effect on what shows up in the table’s body. That’s because the first column is reserved for displaying whatever your element model’s __toString() method returns (the string representation of the element).

So all you need to do is add a __toString() method in your model and return the string representation of your element:

For example:

public function __toString()
{
   return $this->id;
}

Result:

enter image description here

|improve this answer|||||
1

As a workaround you could hide the default title column and add your own:

public function modifyEntryTableAttributes(&$attributes, $source)
{
    if ($source == 'section:2')
    {
        $attributes = array();
        $attributes['myOwnTitle'] = Craft::t('Title');
        $attributes['uri'] = Craft::t('URI');
        $attributes['postDate'] = Craft::t('Post Date');
        $attributes['expiryDate'] = Craft::t('Expiry Date');
    }
}

public function getEntryTableAttributeHtml(EntryModel $entry, $attribute)
{
    switch ($attribute)
    {
        case 'myOwnTitle':
        {
            return 'blabla';
        }

        ...
    }
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.