You are here

function varbase_media_form_entity_embed_dialog_alter in Varbase Media 8.5

Same name and namespace in other branches
  1. 8.7 varbase_media.module \varbase_media_form_entity_embed_dialog_alter()
  2. 8.4 varbase_media.module \varbase_media_form_entity_embed_dialog_alter()
  3. 8.6 varbase_media.module \varbase_media_form_entity_embed_dialog_alter()
  4. 9.0.x varbase_media.module \varbase_media_form_entity_embed_dialog_alter()

Implements hook_form_FORM_ID_alter().

File

./varbase_media.module, line 56
Contains varbase_media.module.

Code

function varbase_media_form_entity_embed_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Only at the embed step.
  if ($form_state
    ->get('step') == 'embed') {

    // Get the entity values and attributes.
    $entity_element = empty($values['attributes']) ? array() : $values['attributes'];
    $entity_element += $form_state
      ->get('entity_element');
    $form_state
      ->set('entity_element', $entity_element);
    $entity = $form_state
      ->get('entity');

    // Get the entity bundle type.
    $bundle_type = $entity
      ->bundle();
    $values = $form_state
      ->getValues();
    switch ($bundle_type) {
      case "image":

        // Adding the alt field to the embed dialog form.
        $form['attributes']['alt'] = [
          '#title' => t('Alternative text'),
          '#type' => 'textfield',
          '#required' => TRUE,
          '#default_value' => $entity->field_media_image->alt,
          '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
        ];

        // Adding the title field to the embed dialog form.
        $form['attributes']['title'] = [
          '#title' => t('Title'),
          '#type' => 'textfield',
          '#default_value' => $entity->field_media_image->title,
          '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
        ];

        // Render the Embed entity.
        $form['entity'] = [
          '#type' => 'item',
          '#markup' => \Drupal::service('renderer')
            ->renderRoot(entity_view($entity, 's03')),
        ];

        // Change the "data align" field from radio buttons to Select list.
        $form['attributes']['data-align']['#type'] = 'select';
        $form['attributes']['data-align']['#wrapper_attributes'] = '';
        $form['attributes']['data-align']['#description'] = t('Choose the positioning of the image.');
        $form['attributes']['data-align']['#weight'] = -10;

        // Add description for the caption field.
        $form['attributes']['data-caption'] += [
          '#description' => t('A caption will be displayed under the image, to describe it in context of your content.'),
        ];

        // Adding the updated alt text to the media entity.
        if (isset($entity_element['alt'])) {
          $entity->field_media_image->alt = $entity_element['alt'];
        }

        // Adding the updated title text to the media entity.
        if (isset($entity_element['title'])) {
          $entity->field_media_image->title = $entity_element['title'];
        }
        if (isset($form['attributes']['data-entity-embed-display-settings'])) {
          $form['attributes']['data-entity-embed-display-settings']['link_url']['#description'] = t('Start typing the title of a piece of content to select it. You can also enter an <br /> internal path such as /node/add or an external URL such as http://example.com.');
        }
        $entity
          ->save();
        break;
      case "video":
      case "video_embed":

        // Render the Embed entity.
        $form['entity'] = [
          '#type' => 'item',
          '#markup' => \Drupal::service('renderer')
            ->renderRoot(entity_view($entity, 's06')),
        ];
        if (isset($form['attributes']['data-align'])) {
          unset($form['attributes']['data-align']);
        }
        if (isset($form['attributes']['data-entity-embed-display-settings'])) {
          unset($form['attributes']['data-entity-embed-display-settings']);
        }
        if (isset($form['attributes']['data-caption'])) {
          unset($form['attributes']['data-caption']);
        }
        if (isset($form['attributes']['data-entity-embed-display'])) {
          $form['attributes']['data-entity-embed-display']['#access'] = FALSE;
          $form['attributes']['data-entity-embed-display']['#default_value'] = 'view_mode:media.original';
        }
        break;
      default:
        if (isset($form['attributes']['data-entity-embed-display-settings'])) {
          unset($form['attributes']['data-entity-embed-display-settings']);
        }
    }
    if (isset($form['revision_log_message'])) {
      $form['revision_log_message']['#access'] = false;
    }
  }
}