Jump to content

Truncate content of fields in admin columns


bluemultimedia
 Share

Recommended Posts

We are currently building a website that is going to use a lot of template fragments with the help of PageTable fields. The editor can build a page out of multiple fragments that will all combine to the full body content of the page. This works really really well. Here is a quick preview:

post-2547-0-57130200-1447851320_thumb.pn

Now, the problem we are seeing ist that not all template fragments are going to have a headline (see first element in the PageTable). This makes it in some cases difficult for editors to find the correct fragment to edit. So we tried to add the body content as a way to show more information. This lead to this:

post-2547-0-03940700-1447851317_thumb.pn

Unfortunately this makes the PageTable even harder to use if long texts are involved. 

Possible other solutions we considered:

  • Make editors set a title for each fragment: they won’t like that and it adds a layer of difficulty
  • Force them to add a headline and add a checkbox if it should not be displayed: additional complexity
  • Add truncated text with stripped out markup to the column instead of the full text

The last item on the list, truncating, is the solution we would prefer. As far as we can see that is not currently possible without extending the functionality. Does anyone have an idea how to best approach this? Is there perhaps a module that could help out? Functionality we overlooked?

This is just a minor issue and we are more than satisfied with the incredible flexibility ProcessWire does allow.  But we want to make life for our editors as easy as possible and this would help a lot to get us to that goal. 

Link to comment
Share on other sites

PageTables do use the markupValue function of fields to determine their content. The function can easily be hooked to manipulate or change the returned markup. 

E.g. in ready.php

$wire->addHookAfter('Fieldtype::markupValue', function($event) {
  $page = $event->arguments(0);
  $field = $event->arguments(1);

  // change the markup if needed
 
  $event->return = $customMarkup
});

Another option could be adding a runtime property to the pages, which holds the right values and use this one in the pagetable.

Link to comment
Share on other sites

This is a great solution, thanks!

My ready.php hook:

$wire->addHookAfter('Fieldtype::markupValue', function($event) {

  $page = $event->arguments(0);
  $field = $event->arguments(1);

  if($field->type == 'FieldtypeTextareaLanguage' or $field->type == 'FieldtypeTextarea') {
    $displayValue = strip_tags($page->get($field));
    if(strlen($displayValue) > 100) {
        $displayValue = substr($displayValue,0,97).'...';
    }  
  }
  else {
      $displayValue = $page->get($field);
  }
  
  $event->return = $displayValue;
});
 

Only problem I am seeing now: after the modal window to edit the pageTable fragment closes, the content refreshes and shows the full content again. The URL that requests the new content is this one:

/processwire/page/edit/?id=1293&InputfieldPageTableField=content_elements&InputfieldPageTableAdd=1294&InputfieldPageTableSort=1294,1295,1296,1297,1298,129

Any idea how I can hook into the refresh? I will try the runtime property option too, just in case.

Edited by Dominik
Link to comment
Share on other sites

It's strange that this would result in different markup, as the rows should be build by the same code. I've no idea why this would happen. But I'd suggest to limit the functionality down by template or something like this, as this hook will currently hit all listers/pagetables that show textareas. Of course only if that's not your intention. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...