You are here

function entity_embed_dialog_form in Entity Embed 7.2

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

Form constructor for the entity embed dialog form.

Allows the user to select and embed an entity.

See also

entity_embed_dialog_form_validate()

3 string references to 'entity_embed_dialog_form'
entity_embed_menu in ./entity_embed.module
Implements hook_menu().
goBack in ./entity_embed.admin.inc
Form submission handler to go back to the previous step of the form.
submitSelectForm in ./entity_embed.admin.inc
Form submission handler that selects an entity and display embed settings.

File

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

Code

function entity_embed_dialog_form($form, &$form_state, $filter_format, $embed_button) {
  $input = $_POST;
  $values = isset($form_state['values']) ? $form_state['values'] : array();

  // Initialize entity element with form attributes, if present.
  $entity_element = empty($values['attributes']) ? array() : $values['attributes'];

  // The default values are set directly from \Drupal::request()->request,
  // provided by the editor plugin opening the dialog.
  if (empty($form_state['entity_element'])) {
    $form_state['entity_element'] = isset($input['editor_object']) ? $input['editor_object'] : array();
  }
  $entity_element += $form_state['entity_element'];
  $entity_element += array(
    'data-entity-type' => $embed_button->entity_type,
    'data-entity-uuid' => '',
    'data-entity-id' => '',
    'data-entity-embed-display' => 'default',
    'data-entity-embed-settings' => array(),
    'data-align' => '',
    'data-caption' => '',
    'data-view-mode' => '',
  );
  if (empty($form_state['step'])) {

    // If an entity has been selected, then always skip to the embed options.
    if (!empty($entity_element['data-entity-type']) && (!empty($entity_element['data-entity-uuid']) || !empty($entity_element['data-entity-id']))) {
      $form_state['step'] = 'embed';
    }
    else {
      $form_state['step'] = 'select';
    }
  }
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = array(
    'entity_embed',
    'drupal.editor.dialog',
  );
  $form['#prefix'] = '<div id="entity-embed-dialog-form">';
  $form['#suffix'] = '</div>';
  switch ($form_state['step']) {
    case 'select':
      $form['#title'] = t('Embed entity');
      $form['attributes']['data-entity-type'] = array(
        '#type' => 'value',
        '#value' => $entity_element['data-entity-type'],
      );
      $label = t('Label');

      // Attempt to display a better label if we can by getting it from
      // the label field definition.
      $entity_info = entity_get_info($entity_element['data-entity-type']);
      if (!empty($entity_info['label'])) {
        $label = check_plain($entity_info['label']);
      }
      $default = '';
      if (!empty($entity_element['data-entity-uuid']) && module_exists('uuid')) {
        $entity = entity_uuid_load($entity_element['data-entity-type'], array(
          $entity_element['data-entity-uuid'],
        ));
        $entity = reset($entity);
      }
      else {
        $entity = entity_load_single($entity_element['data-entity-type'], $entity_element['data-entity-id']);
      }
      if ($entity) {
        $default = entity_label($entity_element['data-entity-type'], $entity);
      }
      $form['attributes']['data-entity-id'] = array(
        '#type' => 'textfield',
        '#title' => $label,
        '#default_value' => $default,
        '#autocomplete_path' => 'entity-embed/autocomplete-entity/' . $filter_format->format . '/' . $embed_button->name,
        '#required' => TRUE,
        '#description' => t('Type label and pick the right one from suggestions. Note that the unique ID will be saved.'),
      );

      // Integrate with the Media module to support selecting files to embed.
      if ($entity_element['data-entity-type'] == 'file' && module_exists('media')) {
        $form['attributes']['data-entity-id'] = array(
          '#type' => 'media',
          '#title' => $label,
          '#default_value' => $entity_element['data-entity-id'],
          '#required' => TRUE,
          '#description' => t('Select a file to embed using the media browser.'),
          '#media_options' => array(
            'global' => array(
              'types' => isset($embed_button->entity_type_bundles) ? $embed_button->entity_type_bundles : array(),
            ),
          ),
        );
      }
      $form['attributes']['data-entity-uuid'] = array(
        '#type' => 'value',
        '#title' => $entity_element['data-entity-uuid'],
      );
      $form['actions'] = array(
        '#type' => 'actions',
      );
      $form['actions']['save_modal'] = array(
        '#attributes' => array(
          'class' => array(
            'button--primary',
          ),
        ),
        '#type' => 'submit',
        '#value' => t('Next'),
        // No regular submit-handler. This form only works via JavaScript.
        '#submit' => array(),
        '#ajax' => array(
          'callback' => 'submitSelectForm',
          'event' => 'click',
        ),
      );
      break;
    case 'embed':
      $form['#title'] = t('Edit embedded entity');
      if (!empty($entity_element['data-entity-uuid']) && module_exists('uuid')) {
        $entity = entity_uuid_load($entity_element['data-entity-type'], array(
          $entity_element['data-entity-uuid'],
        ));
        $entity = reset($entity);
      }
      else {
        $entity = entity_load_single($entity_element['data-entity-type'], $entity_element['data-entity-id']);
      }
      $text = t('Preview selected entity');
      if ($entity) {
        $text = entity_label($entity_element['data-entity-type'], $entity);
      }
      $uri = entity_uri($entity_element['data-entity-type'], $entity);
      $form['entity'] = array(
        '#type' => 'item',
        '#title' => t('Selected entity'),
        '#markup' => l($text, $uri['path'], $uri['options'] + array(
          'attributes' => array(
            'target' => '_blank',
          ),
        )),
      );
      $form['attributes']['data-entity-type'] = array(
        '#type' => 'value',
        '#value' => $entity_element['data-entity-type'],
      );
      $form['attributes']['data-entity-id'] = array(
        '#type' => 'value',
        '#value' => $entity_element['data-entity-id'],
      );
      $form['attributes']['data-entity-uuid'] = array(
        '#type' => 'value',
        '#value' => $entity_element['data-entity-uuid'],
      );
      $options = array();
      $entity_info = entity_get_info($entity_element['data-entity-type']);
      foreach ($entity_info['view modes'] as $view_mode => $info) {
        $options[$view_mode] = check_plain($info['label']);
      }
      $form['attributes']['data-view-mode'] = array(
        '#type' => 'select',
        '#title' => t('Display as'),
        '#options' => $options,
        '#default_value' => $entity_element['data-view-mode'],
        '#required' => TRUE,
        '#ajax' => array(
          'callback' => 'updatePluginConfigurationForm',
          'wrapper' => 'data-entity-embed-settings-wrapper',
          'effect' => 'fade',
        ),
      );
      $form['attributes']['data-entity-embed-settings'] = array(
        '#type' => 'container',
        '#prefix' => '<div id="data-entity-embed-settings-wrapper">',
        '#suffix' => '</div>',
      );
      $form['attributes']['data-embed-button'] = array(
        '#type' => 'value',
        '#value' => $embed_button->name,
      );
      $form['attributes']['data-entity-label'] = array(
        '#type' => 'value',
        '#value' => $embed_button->button_label,
      );

      // @todo: Potentially support 'display plugins' in the future.
      //      $plugin_id = !empty($values['attributes']['data-entity-embed-display']) ? $values['attributes']['data-entity-embed-display'] : $entity_element['data-entity-embed-display'];
      //      if (!empty($plugin_id)) {
      //        if (is_string($entity_element['data-entity-embed-settings'])) {
      //          $entity_element['data-entity-embed-settings'] = Json::decode($entity_element['data-entity-embed-settings'], TRUE);
      //        }
      //        $display = $this->displayPluginManager()->createInstance($plugin_id, $entity_element['data-entity-embed-settings']);
      //        $display->setContextValue('entity', $entity);
      //        $display->setAttributes($entity_element);
      //        $form['attributes']['data-entity-embed-settings'] += $display->buildConfigurationForm($form, $form_state);
      //      }
      $filters = filter_list_format($filter_format->format);

      // When Drupal core's filter_align is being used, the text editor may
      // offer the ability to change the alignment.
      if (isset($entity_element['data-align']) && isset($filters['filter_align']) && $filters['filter_align']->status) {
        $form['attributes']['data-align'] = array(
          '#title' => t('Align'),
          '#type' => 'radios',
          '#options' => array(
            'none' => t('None'),
            'left' => t('Left'),
            'center' => t('Center'),
            'right' => t('Right'),
          ),
          '#default_value' => $entity_element['data-align'] === '' ? 'none' : $entity_element['data-align'],
          '#wrapper_attributes' => array(
            'class' => array(
              'container-inline',
            ),
          ),
          '#attributes' => array(
            'class' => array(
              'container-inline',
            ),
          ),
          '#parents' => array(
            'attributes',
            'data-align',
          ),
        );
      }

      // When Drupal core's filter_caption is being used, the text editor may
      // offer the ability to add a caption.
      if (isset($entity_element['data-caption']) && isset($filters['filter_caption']) && $filters['filter_caption']->status) {
        $form['attributes']['data-caption'] = array(
          '#title' => t('Caption'),
          '#type' => 'textfield',
          '#default_value' => $entity_element['data-caption'] === '' ? '' : $entity_element['data-caption'],
          '#wrapper_attributes' => array(
            'class' => array(
              'container-inline',
            ),
          ),
          '#attributes' => array(
            'class' => array(
              'container-inline',
            ),
          ),
          '#parents' => array(
            'attributes',
            'data-caption',
          ),
        );
      }
      $form['actions'] = array(
        '#type' => 'actions',
      );
      $form['actions']['back'] = array(
        '#type' => 'submit',
        '#value' => t('Back'),
        // No regular submit-handler. This form only works via JavaScript.
        '#submit' => array(),
        '#ajax' => array(
          'callback' => 'goBack',
          'event' => 'click',
        ),
      );
      $form['actions']['save_modal'] = array(
        '#attributes' => array(
          'class' => array(
            'button--primary',
          ),
        ),
        '#type' => 'submit',
        '#value' => t('Embed'),
        // No regular submit-handler. This form only works via JavaScript.
        '#submit' => array(),
        '#ajax' => array(
          'callback' => 'submitEmbedForm',
          'event' => 'click',
        ),
      );
      break;
  }
  return $form;
}