<p>Sprout SEO is a conjunction of different fields when you save an entry it will send the values from the Sprout SEO field (title, description, ogImage, etc..) in the 'metadata' namespace. So, in order to override this value, you can follow the next example:</p>

<pre><code>craft()-&gt;on('entries.onBeforeSaveEntry', function(Event $event) {
    $entry = $event-&gt;params['entry'];

       if($entry-&gt;type['handle'] === 'service'){
          $serviceContent = $entry-&gt;linkToService-&gt;first();

          $seo = $entry-&gt;getContent()-&gt;seo;
          $seo['metadata']['optimizedDescription'] = "Override description value";
          // Update it in the post request
         $entry-&gt;setContentFromPost(array('seo' =&gt; $seo));
      }
});
</code></pre>

<p>In this example I override the 'optimizedDescription' you can see all the properties in the 'sproutseo/models/SproutSeo_MetadataModel.php' File.</p>

<p>Remember that you can also override any value on the front-end using <a href="https://sprout.barrelstrengthdesign.com/craft-plugins/seo/docs/advanced-customization/template-overrides" rel="nofollow noreferrer">Template Overrides</a> or add a Custom Format to specific values like the title or description using twig logic on your <a href="https://sprout.barrelstrengthdesign.com/craft-plugins/seo/docs/core-concepts/element-metadata-field" rel="nofollow noreferrer">Sprout SEO field</a> settings</p>

CC BY-SA 3.0
2017-08-10T20:10:09.740Z
2017-08-10T20:10:09.740Z
Andre L.
<p>Try setting your <code>seo</code>-field like so: </p>

<pre><code>$entry-&gt;getContent()-&gt;seo = ...
</code></pre>

<p>instead of</p>

<pre><code>$event-&gt;params['entry']-&gt;seo = ...
</code></pre>

CC BY-SA 3.0
2017-08-02T09:09:48.183Z
When I try `$event->params['entry']->getContent()->seo = $serviceContent->seo;` I get the same error message
mrsmn
CC BY-SA 3.0
2017-08-02T09:12:19.340Z
Since you set `$entry = $event->params['entry'];` anyway, try using that instead of `$event->params['entry']`, like I already wrote it in my answer.
justussimon91
CC BY-SA 3.0
2017-08-02T09:13:42.770Z
But wont I have to pass the entry back to the Event to make sure it will be saved? Or does Craft 'magically' know that the `$entry` from my 2nd line of code is the one referenced in the params array? Anyway i'll give it a shot
mrsmn
CC BY-SA 3.0
2017-08-02T09:24:36.130Z
The issue still persists I'm afraid. I've updated the question with more information
mrsmn
CC BY-SA 3.0
2017-08-02T11:00:01.540Z
Can you show the part of your code where you get your Metadata Model?
justussimon91
CC BY-SA 3.0
2017-08-02T11:15:14.113Z
Not sure what you mean by that, All the code in play is in the question above I think. If you feel something needs to be added just let me know and I'll gladly supply it.
mrsmn
CC BY-SA 3.0
2017-08-02T11:59:01.253Z
From the new error you're now being confronted with, it would seem that you're trying to access the 'metadata' property of the MetadataModel, that  does apparently not exist. I'm afraid I don't have any particular knowledge about SproutSeo, and there does not seem to be a wiki or anything that let's you see the MetaDataModel's properties.
justussimon91
CC BY-SA 3.0
2017-08-02T09:05:53.713Z
2017-08-02T09:05:53.713Z
justussimon91
<p>I'm trying to modify a field on an entry before it is saved.</p>

<p>What I'm trying to do is use a field from a linked entry to populate the same field on the entry being saved. </p>

<p>From logging I know the event is triggered, and that I can access the <code>seo</code> variable on the linked element.</p>

<p>Edit: The field I'm trying to copy the content from is the SEO field supplied by the sprout SEO plugin. </p>

<p>However when I try to set the field on the main entry with the content from the linked one, I get the following message</p>

<blockquote>
  <p>Property "Craft\EntryModel.seo" is not defined.</p>
</blockquote>

<p>So my question is, how can I correctly set the <code>seo</code> field on the entry being saved? </p>

<hr>

<p>Below you can see the code currently in my plugins <code>init</code> function.</p>

<pre><code>craft()-&gt;on('entries.onBeforeSaveEntry', function(Event $event) {
    $entry = $event-&gt;params['entry'];

    if($entry-&gt;type['handle'] === 'service'){
        $serviceContent = $entry-&gt;linkToService-&gt;first();
        $event-&gt;params['entry']-&gt;seo = $serviceContent-&gt;seo;

        // Tried this aswell, gives second error mentioned below
        $entry-&gt;getContent()-&gt;seo = $serviceContent-&gt;seo;
    }
 );
</code></pre>

<hr>

<p>After trying the answer provided by @justussimon91, the error message changed to: </p>

<blockquote>
  <p>Property "Craft\SproutSeo_MetadataModel.metadata" is not defined.</p>
</blockquote>

<p>Now this might be related to something specific with the Sprout SEO plugin so I'll raise the issue there as well.</p>

<plugin-development><events><plugin-sproutseo><onsaveentry>
Modifying entry with 'onBeforeSaveEntry' event
# Modifying entry with 'onBeforeSaveEntry' event

(Asked by: username_0 on 2017-08-02)

I'm trying to modify a field on an entry before it is saved.
What I'm trying to do is use a field from a linked entry to populate the same field on the entry being saved. 
From logging I know the event is triggered, and that I can access the seo variable on the linked element.
Edit: The field I'm trying to copy the content from is the SEO field supplied by the sprout SEO plugin. 
However when I try to set the field on the main entry with the content from the linked one, I get the following message

Property "Craft\EntryModel.seo" is not defined.

So my question is, how can I correctly set the seo field on the entry being saved? 

Below you can see the code currently in my plugins init function.
craft()->on('entries.onBeforeSaveEntry', function(Event $event) {
    $entry = $event->params['entry'];

    if($entry->type['handle'] === 'service'){
        $serviceContent = $entry->linkToService->first();
        $event->params['entry']->seo = $serviceContent->seo;

        // Tried this aswell, gives second error mentioned below
        $entry->getContent()->seo = $serviceContent->seo;
    }
 );


After trying the answer provided by @username_2, the error message changed to: 

Property "Craft\SproutSeo_MetadataModel.metadata" is not defined.

Now this might be related to something specific with the Sprout SEO plugin so I'll raise the issue there as well.

**Tags:** plugin-development, events, plugin-sproutseo, onsaveentry


---

## 2 answers

### Answer by username_1 on 2017-08-10. Score: 2

Sprout SEO is a conjunction of different fields when you save an entry it will send the values from the Sprout SEO field (title, description, ogImage, etc..) in the 'metadata' namespace. So, in order to override this value, you can follow the next example:
craft()->on('entries.onBeforeSaveEntry', function(Event $event) {
    $entry = $event->params['entry'];

       if($entry->type['handle'] === 'service'){
          $serviceContent = $entry->linkToService->first();

          $seo = $entry->getContent()->seo;
          $seo['metadata']['optimizedDescription'] = "Override description value";
          // Update it in the post request
         $entry->setContentFromPost(array('seo' => $seo));
      }
});

In this example I override the 'optimizedDescription' you can see all the properties in the 'sproutseo/models/SproutSeo_MetadataModel.php' File.
Remember that you can also override any value on the front-end using Template Overrides or add a Custom Format to specific values like the title or description using twig logic on your Sprout SEO field settings


---

### Answer by username_2 on 2017-08-02. Score: 1

Try setting your seo-field like so: 
$entry->getContent()->seo = ...

instead of
$event->params['entry']->seo = ...

> **Comments:**
> **username_0**: When I try `$event->params['entry']->getContent()->seo = $serviceContent->seo;` I get the same error message  
> **username_2**: Since you set `$entry = $event->params['entry'];` anyway, try using that instead of `$event->params['entry']`, like I already wrote it in my answer.  
> **username_0**: But wont I have to pass the entry back to the Event to make sure it will be saved? Or does Craft 'magically' know that the `$entry` from my 2nd line of code is the one referenced in the params array? Anyway i'll give it a shot  
> **username_0**: The issue still persists I'm afraid. I've updated the question with more information  
> **username_2**: Can you show the part of your code where you get your Metadata Model?  
> **username_0**: Not sure what you mean by that, All the code in play is in the question above I think. If you feel something needs to be added just let me know and I'll gladly supply it.  
> **username_2**: From the new error you're now being confronted with, it would seem that you're trying to access the 'metadata' property of the MetadataModel, that  does apparently not exist. I'm afraid I don't have any particular knowledge about SproutSeo, and there does not seem to be a wiki or anything that let's you see the MetaDataModel's properties.  

---

