Answers.Body: <p>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.</p>
  
  <p>To get your plugin code working you should use <code>entries.onBeforeSaveEntry</code> instead of <code>entries.onSaveEntry</code>.</p>
  
  <p>Because when this event is triggered, it's already too late to update <code>$event-&gt;params['entry']</code> properties for your save.</p>
  
  <p>Another thing to change is <code>setContentFromPost()</code>. Change the ContentModel directly<br>
  <code>$entry-&gt;getContent()-&gt;title = $fullTitle;</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-11T17:10:07.747Z
Answers.Text: 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.
Answers.UserDisplayName: Mat
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-11T17:26:12.140Z
Answers.Text: With that I get "CException : Property "Craft\EntryModel.title" is read only" on save.
Answers.UserDisplayName: Mat
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-11T17:37:11.113Z
Answers.Text: Sorry @Mat, it's `$entry->getContent()->title = $fullTitle;`.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-11T16:54:50.370Z
Answers.LastActivityDate: 2015-02-11T18:03:33.097Z
Answers.LastEditDate: 2015-02-11T18:03:33.097Z
Answers.UserDisplayName: carlcs
Body: <p>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:        </p>
  
  <pre><code>  {% 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 %}
  </code></pre>
  
  <p>I have made numerous attempts at shortening this to fit in the character limit, below is my best attempt:</p>
  
  <pre><code>{% 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 %}
  </code></pre>
  
  <p>But it's still getting cut off. Perhaps there is an easy solution for this via some Twig magic?</p>
  
  <p>I then moved to try to do this within a plugin. I put this in my main plugin class:</p>
  
  <pre><code>public function init()
    {
        craft()-&gt;on('entries.onSaveEntry', function(Event $event)
        {
            $entry = $event-&gt;params['entry'];
            if($entry-&gt;section['handle'] == 'dates')
            {
               $start = $entry-&gt;startDate;
               $end = $entry-&gt;endDate;
               $fullTitle = $start . " - " . $end; 
               $entry-&gt;setContentFromPost(array(
                 'title' =&gt; $fullTitle,
               ));
            }
        });
   }
  </code></pre>
  
  <p>I am fairly certain this code is running on save, however my title isn't updating. Any idea what I'm missing?</p>
  
  <p>I have turned off dynamic titles for this section in the entry settings to ensure that wasn't interfering.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-11T16:37:04.577Z
Comments.Text: As far a the limit... are you referring to the title limit of 255?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-11T16:50:53.420Z
Comments.Text: 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.
Comments.UserDisplayName: Mat
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-11T17:01:35.010Z
Comments.Text: @Brad am I right that importing a macro or including another template can't work for dynamic title templates?
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-11T17:51:35.710Z
Comments.Text: @carlcs - Honestly, not 100% sure without setting up a test case.
Comments.UserDisplayName: Brad Bell
Tags: <plugin-development><templating><titles>
Title: How to set a dynamic title in plugin on entry save?
ThreadText: # How to set a dynamic title in plugin on entry save?
  
  (Asked by: username_0 on 2015-02-11)
  
  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.
  
  **Tags:** plugin-development, templating, titles
  
  > **Comments:**
  > **username_1**: As far a the limit... are you referring to the title limit of 255?  
  > **username_0**: Oh snap, thanks username_1. 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.  
  > **username_2**: @username_1 am I right that importing a macro or including another template can't work for dynamic title templates?  
  > **username_1**: @username_2 - Honestly, not 100% sure without setting up a test case.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-02-11. Score: 1 | Accepted answer
  
  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;`
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_0**: With that I get "CException : Property "Craft\EntryModel.title" is read only" on save.  
  > **username_2**: Sorry @username_0, it's `$entry->getContent()->title = $fullTitle;`.  
  
  ---
  
  