LIst Columns
10 posts by 2 authors in: Forums > CMS Builder
Last Post: June 5 (RSS)
By MercerDesign - May 15
Is their a way of showing the name of a file that has been uploaded for each item in a multi record list, so for example if a pdf is loaded up the file name can be displayed in the List Columns in the section editor?
Hi MercerDesign,
You can if you tap into the related filter hook called "listRow_displayValue". Below is an example of using this hook to alter the display value of any list table.
Step 1. Make sure you list the column in the Section/Database Editor >> Editor Tab >> List Columns field. This will show the column for you.
Step 2. Create a plugin file where you then implement the filter hook like shown below. Here we are looking up the specific table (in this case test_table is my table name) and the field name given to the upload field (in my case it is just called 'documents')
addFilter('listRow_displayValue', '_listRow_displayFileNameForUpload', 'admins');
function _listRow_displayFileNameForUpload($displayValue, $tableName, $fieldname, $record) {
// Specify the specific table and field you want to modify
if ($tableName === 'test_table' && $fieldname === 'documents') {
if (isset($record['documents'][0])) {
// Reference the field on that record and the first document in that field
// Then replace its display value. Here I am customizing it to be a "badge" style value as well.
$displayValue = '<span class="badge badge-primary">' . $record['documents'][0]['filename'] . '</span>';
}
}
return $displayValue;
}
Step 3. Customize the line where I am setting up the badge to be the HTML that will replace the display value. I am creating a simple span tag here. Whatever you set displayValue to be will then be displayed. For security reasons, always make sure you are in control of the content here and escape appropriately if need be (if you are doing anything more advanced).
When you display the list for this table then it will render your filename. Since we are referencing '0' it will be the filename of the first upload for the field. Additional uploads will be referenced according (1, 2, 3 etc.)
I hope this helps push you in the right direction to a solution.
Senior Web Programmer
Interactivetools.com
List Columns
By MercerDesign - May 16
Thank you.
I have created a plugin file but I'm not sure I've done it correctly or loaded it up to the correct place, nothing has changed but I can see it in the Developer's Plugin Hook List.
List Columns
Hi MercerDesign,
I have run and tested this code before I posted, so I am sure the actual code works. Make sure you have the following set up:
- Make sure the plugin file you created is activated and is in the active plugin section.
- Make sure you replace the table name and the field name to reflect your table and field you want to target. In my example I used "test_table" and "documents" but yours will be different.
- The field name is referenced in a couple places so make sure all have been replaced.
If you have done all these things and still see no change, you can always post your entire plugin file and I can take a look. If all is good I should be able to take your file, change the table and fieldname as appropriate and it should just work.
Let me know how things go. :)
Senior Web Programmer
Interactivetools.com
List Columns
By MercerDesign - May 19
Hi, all the required changes have been made to suit my CMS but I don't get an option to activate the plugin, have you got an example plugin file I can copy to make sure I have done it correctly?
List Columns
Hi MercerDesign,
Well if you see it in the plugin list, you have done it correctly. If you look to the right of the plugin name in the list you should see either "Activate" or "Deactivate" as a link. If you see "Activate" click it to turn it on. It should then appear in the top list. If it is deactivated, it will appear in the bottom list.
The code I presented you is pretty much the entire plugin file other than the header to tell CMSB that the file is a plugin.
Senior Web Programmer
Interactivetools.com
List Columns
By MercerDesign - May 20 - edited: May 20
Thank you. I had missed the header code to tell the CMS it was a plugin (sorry I've not created a plugin file before). This now works.
List Columns
By MercerDesign - June 4
I have this working (thank you), but I can't get more than one document to show in the list, can you help please? This is my code:
<?php
/*
Plugin Name: List Columns
Description:
*/
addFilter('listRow_displayValue', '_listRow_displayFileNameForUpload', 'admins');
function _listRow_displayFileNameForUpload($displayValue, $tableName, $fieldname, $record) {
// Specify the specific table and field you want to modify
if ($tableName === 'live_stream' && $fieldname === 'documents') {
if (isset($record['documents'][0])) {
// Reference the field on that record and the first document in that field
// Then replace its display value. Here I am customizing it to be a "badge" style value as well.
$displayValue = '<span class="badge badge-primary">' . $record['documents'][0]['filename'] . '</span>';
}
}
return $displayValue;
}
List Columns
Hi MercerDesign,
Looking at the code you will see that $record['documents'] is an array of values and we are referencing just the first one with [0]. So if you want to see more than just the first, you can loop through the documents.
function _listRow_displayFileNameForUpload($displayValue, $tableName, $fieldname, $record) {
// Specify the specific table and field you want to modify (replace the table name and 'documents' with your field name)
if ($tableName === 'test_table' && $fieldname === 'documents') {
// Check if the record as our field name and it is an array with at least one document
if (isset($record['documents']) && is_array($record['documents']) && count($record['documents']) > 0) {
$displayValue = '';
// Loop through each document in the 'documents' field
foreach ($record['documents'] as $document) {
// Reference the field on that record and the 'filename' key
// Add to the display value. Here I am customizing it to be a "badge" style value as well.
$displayValue .= '<span class="badge badge-primary">' . $document['filename'] . '</span>';
}
}
}
return $displayValue;
}
Notice here that we introduced a foreach that loops through that array and for each one we are tacking it onto the $displayValue. Hopefully you see how this is working out. Again I have tested this code myself so should work fine once you make the modifications for your situation.
I hope that helps!
Senior Web Programmer
Interactivetools.com