1

I have a section of events and am trying to automatically set a readable title on the event entry in the CMS ( such as Jan 5 - 12, 2015 or Jan 5, 2013 - Jan 10, 2016 ). I realize I can do this via a Dynamic Title on the entry type, however my twig logic gets cut off, it seems it won't fit in the allowed character limit (220 ?). My twig code looks like:

  {% set start = object.startDate %}
  {% set end = object.endDate %}

  {{start|date('F j')}}
  {% if end %}
    {% if start|date('Y') == end|date('Y') %}
      {% if start|date('F') == end|date('F') %}
         – {{end|date('j, Y')}}
      {% else %}
        – {{end|date('F j, Y')}}
      {% endif %}
    {% else %}
      {{start|date(', Y')}} – {{end|date('F j, Y')}}
    {% endif %}
  {% else %}
    {{ start|date(', Y') }}
  {% endif %}

I have made numerous attempts at shortening this to fit in the character limit, below is my best attempt:

{% set s,e = object.startDate,object.endDate %}{{s|date('F j')}}{% if e %}{% if s|date('Y')==e|date('Y') %}{% if s|date('F')==e|date('F') %}–{{e|date('j, Y')}}{% else %} – {{e|date('F j, Y')}}{% endif %}{% else %}{{s|date(', Y')}} – {{e|date('F j, Y')}}{% endif %}{% endif %}

But it's still getting cut off. Perhaps there is an easy solution for this via some Twig magic?

I then moved to try to do this within a plugin. I put this in my main plugin class:

public function init()
  {
      craft()->on('entries.onSaveEntry', function(Event $event)
      {
          $entry = $event->params['entry'];
          if($entry->section['handle'] == 'dates')
          {
             $start = $entry->startDate;
             $end = $entry->endDate;
             $fullTitle = $start . " - " . $end; 
             $entry->setContentFromPost(array(
               'title' => $fullTitle,
             ));
          }
      });
 }

I am fairly certain this code is running on save, however my title isn't updating. Any idea what I'm missing?

I have turned off dynamic titles for this section in the entry settings to ensure that wasn't interfering.

  • As far a the limit... are you referring to the title limit of 255? – Brad Bell Feb 11 '15 at 16:37
  • Oh snap, thanks Brad. I was able to squeeze it in to 255 chars. Still interested in figuring out how to do it in a plugin for more control. – Mat Feb 11 '15 at 16:50
  • @Brad am I right that importing a macro or including another template can't work for dynamic title templates? – carlcs Feb 11 '15 at 17:01
  • @carlcs - Honestly, not 100% sure without setting up a test case. – Brad Bell Feb 11 '15 at 17:51
1

To get more code into the template field that generates the title I tried importing a macro, but this didn't work. So squeezing it into the 255 chars is probably our only chance here. So it would be in fact better to do it with plugin.

To get your plugin code working you should use entries.onBeforeSaveEntry instead of entries.onSaveEntry.

Because when this event is triggered, it's already too late to update $event->params['entry'] properties for your save.

Another thing to change is setContentFromPost(). Change the ContentModel directly
$entry->getContent()->title = $fullTitle;

  • Thanks - I tried both onBeforeSaveEntry and onSaveEntry with no success. When I log both I only get the original title of the entry, not the custom title I'm trying to set with $fullTitle. There must be some extra step I'm missing. – Mat Feb 11 '15 at 17:10
  • With that I get "CException : Property "Craft\EntryModel.title" is read only" on save. – Mat Feb 11 '15 at 17:26
  • Sorry @Mat, it's $entry->getContent()->title = $fullTitle;. – carlcs Feb 11 '15 at 17:37

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.