1

I am trying to put a custom validation on node form which has paragraph attached to it. The problem is I am not getting paragraph field values available in form_state of my validation handler.

I just see the delta of entites when I do a debug on form_state

enter image description here

As in the screenshot field_asset the entity_reference_revision referencing a media entity, I only see two entities attached in form_state in custom submit handler on node_form but how can I get value of those refernced entities?

Update: I realised it was not paragraph entity but media entity.

7
  • $node->field_asset->getValue() have you tried this. Have you tried to get value method ??. I am assuming your field to be a entity reference field with multiple values . Jan 13, 2017 at 11:56
  • node is not created yet during form validation as it could be form edit/create.
    – arpitr
    Jan 13, 2017 at 11:57
  • are you performing validation on node_content_type_edit_form ?? Jan 13, 2017 at 11:59
  • $form_state->getValue('field_asset'); Have you tried this . Is your validation called ?? Jan 13, 2017 at 12:01
  • The debug output is from validation so that is sure it is getting called, the getValue will return the value as shown in the output, however, paragraph is set of different fields, I am looking a way to get hold of nested values.
    – arpitr
    Jan 13, 2017 at 12:27

1 Answer 1

0

Digging the form_state I found the user submitted values for embedded media entites lies at form state storage. My validation code now looks like

/**
 * Implememts hook_form_alter().
 */
function mymodulename_form_alter(&$form, \Drupal \Core\Form\FormStateInterface $form_state, $form_id) {
  $operative_form = array(
    'my_node_add_form',
    'my_node_edit_form'
  );
  if (!in_array($form_id, $operative_form)) {
    return;
  }
  $form['#validate'][] = '_mymodulename_validate_element_node_form';
}

/**
 * Custom validation handler for EDP.
 */
function _mymodulename_validate_element_node_form($form, $form_state) {
  $form_storage = $form_state->getStorage();
  $form_storage_embed_entities = $form_storage['inline_entity_form'];
  foreach ($form_storage_embed_entities as $key => $value) {
    $media_entities = $value['entities'];
    foreach ($media_entities as $media_entity) {
      if ($media_entity['entity'] instanceof \Drupal\Core\Entity\EntityInterface) {
        if ($media_entity['entity']->getEntityTypeId() == 'media') {
          $asset_type[$media_entity['entity']->field_asset_type->value] = $media_entity['entity']->field_asset_type->value;
        }
      }
      if (count($asset_type) > 1) {
        // early bail.
        break;
      }
    }
    // Validation condition goes here.
    if ($validation_condition) {
      $form_state->setErrorByName('field_asset', t('Did you try turning it on and off.'));
    }
  }
}

Your Answer

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

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