Answers.Body: <p>If you set the slug directly on the entry element, it should work!</p>
  
  <pre><code>public function init() {
      craft()-&gt;on('entries.saveEntry', function(Event $event) {
          $entry = $event-&gt;params['entry'];
          $entryId = $entry-&gt;id;
  
          $newTitle = str_replace('Name: ', $entryId . ' - ', $entry-&gt;title);
  
          $entry-&gt;getContent()-&gt;title = $newTitle;
  
          $entry-&gt;slug = $entryId;
  
          craft()-&gt;elements-&gt;saveElement($entry);
      });
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-05T21:26:13.627Z
Answers.Text: Awesome! That works perfectly. Thank you!
Answers.UserDisplayName: Gavin Anderegg
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-05T21:22:32.417Z
Answers.LastActivityDate: 2015-10-05T21:22:32.417Z
Answers.UserDisplayName: Aaron Berkowitz
Body: <p>I have a plugin where I'm creating custom entries based on user submitted data. Saving these entries works well. As does modifying their titles after saving to add the entry's ID to the title (using <code>craft()-&gt;on('entries.saveEntry', function(Event $event) { ... }</code>). The problem I'm having now is that I'd like to also update the slug at this point, so that each entry's slug is just its ID.</p>
  
  <p>I've tried using:</p>
  
  <pre><code>$entry-&gt;setContentFromPost(array(
      'slug' =&gt; $entryId,
  ));
  </code></pre>
  
  <p>and</p>
  
  <pre><code>$entry-&gt;getContent()-&gt;slug = $entryId;
  </code></pre>
  
  <p>but neither do anything — even though the title change is getting saved successfully. Either I'm missing something, or the slug is handled differently than both entry fields and entry base content.</p>
  
  <p><strong>My question:</strong> How do I update the entry slug from a plugin to be just the entry's ID?</p>
  
  <hr>
  
  <p>Here's the code I currently have in my plugin's base class:</p>
  
  <pre><code>public function init()
  {
      craft()-&gt;on('entries.saveEntry', function(Event $event)
      {
          $entry = $event-&gt;params['entry'];
          $entryId = $entry-&gt;id;
  
          $newTitle = str_replace('Name: ', $entryId . ' - ', $entry-&gt;title);
  
          $entry-&gt;getContent()-&gt;title = $newTitle;
  
          $entry-&gt;setContentFromPost(array(
              'slug' =&gt; $entryId,
          ));
  
          // Here I use elements-&gt;saveElement() because using entries-&gt;saveEntry 
          // cased an infinite loop.
          // It would be great to have this sanity checked as well, please :)
          craft()-&gt;elements-&gt;saveElement($entry);
      });
  }
  </code></pre>
  
Tags: <plugin-development><php><slug><events>
Title: Setting an entry slug from a pluign
ThreadText: # Setting an entry slug from a pluign
  
  (Asked by: username_0 on 2015-10-05)
  
  I have a plugin where I'm creating custom entries based on user submitted data. Saving these entries works well. As does modifying their titles after saving to add the entry's ID to the title (using `craft()->on('entries.saveEntry', function(Event $event) { ... }`). The problem I'm having now is that I'd like to also update the slug at this point, so that each entry's slug is just its ID.
  
  I've tried using:
  
  ```
  $entry->setContentFromPost(array(
      'slug' => $entryId,
  ));
  
  ```
  
  and
  
  ```
  $entry->getContent()->slug = $entryId;
  
  ```
  
  but neither do anything — even though the title change is getting saved successfully. Either I'm missing something, or the slug is handled differently than both entry fields and entry base content.
  
  **My question:** How do I update the entry slug from a plugin to be just the entry's ID?
  
  ---
  
  Here's the code I currently have in my plugin's base class:
  
  ```
  public function init()
  {
      craft()->on('entries.saveEntry', function(Event $event)
      {
          $entry = $event->params['entry'];
          $entryId = $entry->id;
  
          $newTitle = str_replace('Name: ', $entryId . ' - ', $entry->title);
  
          $entry->getContent()->title = $newTitle;
  
          $entry->setContentFromPost(array(
              'slug' => $entryId,
          ));
  
          // Here I use elements->saveElement() because using entries->saveEntry 
          // cased an infinite loop.
          // It would be great to have this sanity checked as well, please :)
          craft()->elements->saveElement($entry);
      });
  }
  
  ```
  
  **Tags:** plugin-development, php, slug, events
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-10-05. Score: 2 | Accepted answer
  
  If you set the slug directly on the entry element, it should work!
  
  ```
  public function init() {
      craft()->on('entries.saveEntry', function(Event $event) {
          $entry = $event->params['entry'];
          $entryId = $entry->id;
  
          $newTitle = str_replace('Name: ', $entryId . ' - ', $entry->title);
  
          $entry->getContent()->title = $newTitle;
  
          $entry->slug = $entryId;
  
          craft()->elements->saveElement($entry);
      });
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Awesome! That works perfectly. Thank you!  
  
  ---
  
  