Adding a Custom Field to the Structure Page Control Panel

A recent project with POWERSHiFTER had us designing an Expression Engine-driven site with a ridiculous amount of content (110,000 words / 500+ printed pages). Luckily the content was very well organized and came complete with internal addressing system for each of the sections to improve the quality of life for out copy editors.

Out-of-the-box page hierarchy on the Structure control panel

Now for all of our Expression Engine sites we immediately install Structure, a badass module that completely reinvents how EE handles pages. Basically, it’s the best thing ever.. it’s got an intuitive design and saves us (me) from messing around trying to hack a “page structure” into EE, and lists site content pages in a hierarchy on a single view on the admin control panel which makes it super-easy to find or edit pages. Awesome! But even with all those perks, managing all that content in Structure became a bit cumbersome as everything in our copy was referenced by the “internal address” I mentioned above, not by the page titles that appear on the Structure control panel.

The solution? Add our internal document address system custom field to the Structure control panel page!

After adding custom field data to the page hierarchy

Keep in mind that this should work for any custom field attached to a Structure page (provided the field exists), but I’m not making any guarantees or warranty that this will work.. even though it should. Basically, it works on my machine using an install of ExpressionEngine 2.1.3 but, as with everything in life, YMMV.

Further Warning: This will modify the core code for Structure. Don’t expect Travis or Jack (the guys behind Structure) to support your extension if these modifications break your site, cause you mental anguish, or murder your goldfish. You’ve been warned.

Step 0:

FOR GOODNESS SAKE, BACK UP EVERYTHING.

Step 1:

Get the Field ID for the custom field you want to include on the Structure Control Panel page. You can get this by going to Admin -> Channel Administration -> Custom Fields. Click “Add/Edit Custom Fields” for the appropriate Field Group, then click on the custom field you will be using.

Now look at the URL, you should see an address like this:
http://csrdev.powershiftermedia.com/admin/index.php?S=1234567890deadbeef&D=cp&C=admin_content&M=field_edit&field_id=21&group_id=5

Write down the number that comes after “field_id=” in the URL; in this case it’s “21”. We will be using this later.

Step 2

Open the file “expressionengine/third_party/stricture/sql.structure.php” and find the “get_data()” function, on-or-around line 83. We are going to update the SQL query used by this function.

The SQL query at the time of writing is the following:

[php title=”Original SQL”]
$sql = "SELECT node.*, (COUNT(parent.entry_id) – 1) AS depth, expt.title, expt.status
FROM exp_structure AS node
INNER JOIN exp_structure AS parent
ON node.lft BETWEEN parent.lft AND parent.rgt
INNER JOIN exp_channel_titles AS expt
ON node.entry_id = expt.entry_id
WHERE parent.lft > 1
AND node.site_id = {$this->site_id}
AND parent.site_id = {$this->site_id}
GROUP BY node.entry_id
ORDER BY node.lft";
[/php]

And we’re going to update it to the following:

[php highlight=”2,8″ title=”Modified SQL Query”]
$sql = "SELECT node.*, (COUNT(parent.entry_id) – 1) AS depth, expt.title, expt.status
,expd.field_id_21 AS content_address
FROM exp_structure AS node
INNER JOIN exp_structure AS parent
ON node.lft BETWEEN parent.lft AND parent.rgt
INNER JOIN exp_channel_titles AS expt
ON node.entry_id = expt.entry_id
INNER JOIN exp_channel_data as expd ON node.entry_id = expd.entry_id
WHERE parent.lft > 1
AND node.site_id = {$this->site_id}
AND parent.site_id = {$this->site_id}
GROUP BY node.entry_id
ORDER BY node.lft";
[/php]

Notice the additions I’ve made on the 2nd and 8th lines. Let me explain:

  • Line 2: Replace “21” with the Field ID you noted from Step 1. This will return the data you care about. You can also add “AS your_field_name” bit to your SQL to help you identify the field in the next step.
  • Line 8: This INNER JOIN connects the page title we’re returning to the custom field data for the page. You don’t need to edit this line.

Save and close the file.

Step 3:

Open the file “expressionengine/third_party/structure/views/index.php” and find following code (on or around line 69):

[php]
<a class="page-edit" href="<?=$edit_url;?>"><?=$page[‘title’];?></a>
[/php]

Now update the code to add in your custom field data, similar to the following:

[php]
<a class="page-edit" href="<?=$edit_url;?>"><?=$page[‘content_address’];?>&nbsp;<?=$page[‘title’];?></a>
[/php]

Save and close the file. Done!

Leave a Reply