0

For my use case, this question revolves around the entities node and paragraph.

Fields in my projects are created under the following structure:

  • content_h2
  • paragraph_h2
  • content_p
  • paragraph_p
  • ...

etc...

For example, I'd like to create field_name on a node, check if it exists in paragraph entity storage and if not create it. And the other way around.

I've had some success but it seems like more coding than needed.

Does a hook exist for node add field?

Example form class: field-ui-field-storage-add-form.

  • Aren’t storage and instances stored differently for different entities? I don’t see user fields for nodes or paragraphs and Vice versa. – Kevin May 22 at 21:40
  • @Kevin precisely, so OP wants to avoid two of the same. – No Sssweat May 22 at 21:42
  • This looks like it is about the config entity FieldStorageConfig, see drupal.stackexchange.com/questions/222260/… – 4k4 May 22 at 22:55
  • 1
    which like any entity has plenty of CRUD hooks, see api.drupal.org/api/drupal/… – 4k4 May 23 at 7:32
1

By the way, all the hooks are listed here there doesn't appear one that fits the task tho.

You can always add a custom validation to the field add form.

/*
 * Implements hook_form_FORM_ID_alter()
 */
function mymodule_form_field_ui_field_storage_add_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $form['#validate'][] = 'mymodule_custom_add_field_validation';
}

/*
 * Custom Validate Callback
 */
function mymodule_custom_add_field_validation(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  // the code that performs the check
  // if (......) {
  //   $form_state->setErrorByName('label',
  //     t('This field already exists in another entity'));
  // }
}

Your Answer

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

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