You are here

function entity_embed_dialog_form_validate in Entity Embed 7.2

Same name and namespace in other branches
  1. 7.3 entity_embed.admin.inc \entity_embed_dialog_form_validate()
  2. 7 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 287
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);
            }
          }
        }
        else {
          form_set_error('data-entity-id', t('Unable to load @type entity @id.', array(
            '@type' => $entity_type,
            '@id' => $title,
          )));
        }
      }
      break;
  }
}