You are here

function varbase_media_form_entity_embed_dialog_alter in Varbase Media 9.0.x

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.5 varbase_media.module \varbase_media_form_entity_embed_dialog_alter()
  4. 8.6 varbase_media.module \varbase_media_form_entity_embed_dialog_alter()

Implements hook_form_FORM_ID_alter().

File

./varbase_media.module, line 228
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 = [];
    $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();
    $builder = \Drupal::entityTypeManager()
      ->getViewBuilder($entity
      ->getEntityTypeId());
    switch ($bundle_type) {

      // ----------------------------------------------------------------------.
      case "image":

        // Image Media for review.
        $media_view_mode = $builder
          ->view($entity, 's03');
        $media_markup = \Drupal::service('renderer')
          ->renderRoot($media_view_mode);

        // Render the Embed entity.
        $form['entity'] = [
          '#type' => 'item',
          '#markup' => $media_markup,
        ];

        // 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 "remote_video":

        // Video Media for review.
        $media_view_mode = $builder
          ->view($entity, 's06');
        $media_markup = \Drupal::service('renderer')
          ->renderRoot($media_view_mode);

        // Render the Embed entity.
        $form['entity'] = [
          '#type' => 'item',
          '#markup' => $media_markup,
        ];
        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;

      // ----------------------------------------------------------------------.
      case "gallery":

        // Gallery Media for review.
        $media_view_mode = $builder
          ->view($entity, 'browser_teaser');
        $media_markup = \Drupal::service('renderer')
          ->renderRoot($media_view_mode);

        // Render the Embed entity.
        $form['entity'] = [
          '#type' => 'item',
          '#markup' => '<div class="gallery-entity-embed-dialog-step--embed"><div class="media-library-item">' . $media_markup . '</div></div>',
        ];

        // Render the Embed entity.
        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.full';
        }
        break;
      default:
        if (isset($form['attributes']['data-entity-embed-display-settings'])) {
          unset($form['attributes']['data-entity-embed-display-settings']);
        }
    }
    if (Drupal::service('module_handler')
      ->moduleExists('blazy')) {

      // Attach the Blazy library.
      $form['#attached']['library'][] = 'blazy/load';
    }

    // No revision information or revision log message.
    if (isset($form['revision_information'])) {
      $form['revision_information']['#disabled'] = TRUE;
      $form['revision_information']['#attributes']['style'][] = 'display: none;';
      $form['revision_information']['#prefix'] = '<div style="display: none;">';
      $form['revision_information']['#suffix'] = '</div>';
    }

    // Hide revistion.
    if (isset($form['revision'])) {
      $form['revision']['#default_value'] = TRUE;
      $form['revision']['#disabled'] = TRUE;
      $form['revision']['#attributes']['style'][] = 'display: none;';
    }

    // Hide revistion log message.
    if (isset($form['revision_log_message'])) {
      $form['revision_log_message']['#disabled'] = TRUE;
      $form['revision_log_message']['#attributes']['style'][] = 'display: none;';
    }
  }
}