4

I've been tasked with adding a column to the Entries screen to show some custom data that gets saved against each new post. To do this, I've edited craft/app/elementtypes/EntryElementType.php and added a new table attribute, which does what I need and shows the column.*

However, I'm wary though that updating Craft involves removing the /app directory and replacing it with the one from the latest release.

Is there an advised way I should make tweaks to core files like the one I mentioned? Or, is the only solution to do something like...

  • Add PHP comments around any core code I tweak
  • When preparing for an update, compare the old /app against the new one in GitHub
  • From there, search through the changes to find the comments I've added
  • For each comment I find, I should manually recreate the changes in the correct places within the new version of /app

* Just to clarify, I was using an older version of Craft when I wrote this post. Having updated to 2.6, I now see that choosing which columns to show on the Entries screen is a baked-in feature. Nice :)

11

You should never edit the core files (i.e. anything below /app), because whatever changes you add might be overwritten any time Craft is updated – or even worse, your amendments might conflict with new features added in Craft updates to a different part of the codebase; crashing your entire site. Seriously, don't do it.

Whenever you need Craft to do something that it doesn't do out of the box, your options are:

– Installing an off-the-shelf plugin, of which there are many
Writing a plugin yourself (which shouldn't be too hard, if you have PHP experience)
Finding someone else to build your custom plugin
Adding a feature request

If you do need to write a custom plugin, probably the most important thing to know about are hooks and events, which offer ways to hook into and act upon various parts of the Craft internals and request lifecycle.

Specifically, I believe your current use case could be easily solved by creating a small custom plugin with the defineAdditionalEntryTableAttributes hook.

Here's how that could look, assuming your custom property is named customProperty (the following would go into your plugin's primary class, e.g. myplugin/MyPluginPlugin.php):

public function defineAdditionalEntryTableAttributes()
{
    return array(
        'customProperty' => "Custom label",
    );
}

With the above, you'll be able to simply add your custom property to the relevant element indexes via the little cogwheel below your Sections, under Entries inside the CP.

If you need more control over how the custom property renders (for instance, if the property returns a non-String value, you want to wrap the property value in some HTML, or you need to account for the custom property not being set for all entries, etc), you can use the getEntryTableAttributeHtml hook to refine things:

public function getEntryTableAttributeHtml(EntryModel $entry, $attribute)
{
    if ($attribute == 'customProperty' && isset($entry->customProperty))
    {
        return '<span class="some-class-name">'.$entry->customProperty[0].'</span>';
    }
}
  • Many thanks for the detailed answer! I will take a look into the resources you've advised. – Chris Jan 3 '17 at 15:14

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.