You are here

function entity_embed_dialog_form_validate in Entity Embed 7.3

Same name and namespace in other branches
  1. 7 entity_embed.admin.inc \entity_embed_dialog_form_validate()
  2. 7.2 entity_embed.admin.inc \entity_embed_dialog_form_validate()

Form validation handler for entity_embed_dialog_form().

See also

entity_embed_dialog_form()

File

./entity_embed.admin.inc, line 313
Admin page callbacks for the entity_embed module.

Code

function entity_embed_dialog_form_validate($form, &$form_state) {
  $values = $form_state['values'];
  switch ($form_state['step']) {
    case 'select':
      if ($entity_type = $values['attributes']['data-entity-type']) {
        $title = trim($values['attributes']['data-entity-id']);
        $entity_info = entity_get_info($entity_type);

        // Prevent errors if the entity type has no label key.
        if (empty($entity_info['entity keys']['label'])) {
          form_set_error('data-entity-id', t('Unable to find label for @type entity @id.', array(
            '@type' => $entity_type,
            '@id' => $title,
          )));
        }

        // The entity ID may be either the ID (integer) of the entity or the
        // entity's title (string).
        if (is_numeric($title)) {
          $entities = entity_load($entity_type, array(
            $title,
          ));
        }
        else {
          $entities = entity_load($entity_type, FALSE, array(
            $entity_info['entity keys']['label'] => array(
              $title,
            ),
          ));
        }
        $entity = reset($entities);
        if (!empty($entity)) {
          list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
          if (!entity_access('view', $entity_type, $entity)) {
            form_set_error('data-entity-id', t('Unable to access @type entity @id.', array(
              '@type' => $entity_type,
              '@id' => $title,
            )));
          }
          else {
            form_set_value($form['attributes']['data-entity-id'], $id, $form_state);
            if (isset($entity->uuid)) {
              form_set_value($form['attributes']['data-entity-uuid'], $entity->uuid, $form_state);
            }
            else {
              form_set_value($form['attributes']['data-entity-uuid'], '', $form_state);
            }

            // Ensure that at least one Entity Embed Display plugin is present
            // before proceeding to the next step. Rasie an error otherwise.
            $embed_button = $form['#embed_button'];

            // Determine the available Display plugins for the current entity type.
            $available_plugins = entity_embed_get_entity_field_formatters($entity_type, $entity);

            // Retrieve the list of allowed Entity Embed Display plugins.
            $allowed_plugins = $embed_button->display_plugins;

            // If list of allowed options is empty, it means that all plugins are
            // allowed. Else, take the intersection of allowed and available
            // plugins.
            $display_plugin_options = empty($allowed_plugins) ? $available_plugins : array_intersect_key($available_plugins, $allowed_plugins);

            // If no plugin is available after taking the intersection,
            // raise error. Also log an exception.
            if (empty($display_plugin_options)) {
              form_set_error('data-entity-id', t('No display options available for the selected entity. Please select another entity.'));
              watchdog('entity_embed', 'No display options available for "@type:" entity "@id" while embedding using button "@button". Please ensure that at least one Entity Embed Display plugin is allowed for this embed button which is available for this entity.', array(
                '@type' => $entity_type,
                '@id' => $id,
                '@button' => $embed_button->admin_title,
              ), WATCHDOG_WARNING);
            }
          }
        }
        else {
          form_set_error('data-entity-id', t('Unable to load @type entity @id.', array(
            '@type' => $entity_type,
            '@id' => $title,
          )));
        }
      }
      break;
    case 'embed':

      // @todo: Display plugin validation.
      break;
  }
}