3

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 @justussimon91, 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.

2 Answers 2

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

1

Try setting your seo-field like so:

$entry->getContent()->seo = ...

instead of

$event->params['entry']->seo = ...
7
  • When I try $event->params['entry']->getContent()->seo = $serviceContent->seo; I get the same error message
    – mrsmn
    Commented Aug 2, 2017 at 9:09
  • Since you set $entry = $event->params['entry']; anyway, try using that instead of $event->params['entry'], like I already wrote it in my answer. Commented Aug 2, 2017 at 9:12
  • 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
    Commented Aug 2, 2017 at 9:13
  • The issue still persists I'm afraid. I've updated the question with more information
    – mrsmn
    Commented Aug 2, 2017 at 9:24
  • Can you show the part of your code where you get your Metadata Model? Commented Aug 2, 2017 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.