1

I have a custom field type, that generates a value in a preSave hook.

class AutoGeneratedItem extends FieldItemBase implements FieldItemInterface
{
  // ** Snip **

  public function preSave()
  {
    // Set value here
  }

  // ** Snip **
}

Apart from that, I have a custom module, which has it's own presave hook. Where I would like to use the auto generated value of the custom field type.

function HOOK_entity_presave($entity)
{
    $autoValue = $entity->auto_generated_field->value;
}

However, in the presave hook the value of the auto generated field is still NULL, how can I make my presave hook fire after the preSave in the custom FieldType?

  • Since your second module obviously depends on your first module, you could implement a custom hook and invoke that from the first preSave(). This makes sure the first module calls the second one (it will always run when requested). (The linked example is not really a great source, but the suggestion remains valid ;) ) – Neograph734 Jan 6 '17 at 20:14
  • To be clear, preSave isn't a hook, it's default functionality to the Field Plugin System, i've added a link to the function in the api documents in my post above (api.drupal.org/api/drupal/…). – pjcarly Jan 6 '17 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.