You are here

varbase_media.module in Varbase Media 8.6

File

varbase_media.module
View source
<?php

/**
 * @file
 * Contains varbase_media.module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\ds\Ds;
use Drupal\entity_browser_generic_embed\OverrideHelper as Override;
use Drupal\varbase_media\Plugin\media\Source\VarbaseMediaFile;
use Drupal\varbase_media\Plugin\media\Source\VarbaseMediaImage;
use Drupal\varbase_media\Plugin\media\Source\VarbaseMediaAudioFile;
use Drupal\varbase_media\Plugin\media\Source\VarbaseMediaVideoFile;
use Drupal\varbase_media\Plugin\media\Source\VarbaseMediaRemoteVideo;
use Drupal\Component\Utility\Html;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Language\LanguageInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Url;

/**
 * Implements hook_preprocess_field().
 */
function varbase_media_preprocess_field(&$variables) {

  // Provide an extra variable to the field template when the field uses
  // a formatter of type 'oembed'
  $resource_fetcher = \Drupal::service('media.oembed.resource_fetcher');
  $url_resolver = \Drupal::service('media.oembed.url_resolver');
  $iframe_url_helper = \Drupal::service('media.oembed.iframe_url_helper');
  if ($variables['element']['#formatter'] == 'oembed') {
    $entity = $variables['element']['#object'];
    $view_mode = $variables['element']['#view_mode'];
    $field_name = $variables['element']['#field_name'];
    $bundle = $variables['element']['#bundle'];

    // get the field formatter settings...
    $entity_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
    $field_display = $entity_display
      ->getComponent($field_name);
    if ($bundle == "remote_video") {
      $max_width = $field_display['settings']['max_width'];
      $max_height = $field_display['settings']['max_height'];
      $item = $variables['element']["#items"]
        ->first();
      $main_property = $item
        ->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getMainPropertyName();
      $value = $item->{$main_property};
      $resource_url = $url_resolver
        ->getResourceUrl($value, $max_width, $max_height);
      $resource = $resource_fetcher
        ->fetchResource($resource_url);
      $provider = $resource
        ->getProvider()
        ->getName();
      $url = Url::fromRoute('media.oembed_iframe', [], [
        'query' => [
          'url' => $value,
          'max_width' => $max_width,
          'max_height' => $max_height,
          'type' => "remote_video",
          'provider' => strtolower($provider),
          'view_mode' => $view_mode,
          'hash' => $iframe_url_helper
            ->getHash($value, $max_width, $max_height, $provider, $view_mode),
        ],
      ]);
      $variables['items'][0]['content']['#attributes']['src'] = $url
        ->toString();
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function varbase_media_preprocess_media_oembed_iframe(&$variables) {

  // Send variables for all oembed iframe theme template.
  $query = \Drupal::request()->query;
  $variables['type'] = $query
    ->get('type');
  $variables['provider'] = $query
    ->get('provider');
  $variables['view_mode'] = $query
    ->get('view_mode');
  $variables['base_path'] = base_path();
  $variables['varbase_media_path'] = \Drupal::service('module_handler')
    ->getModule('varbase_media')
    ->getPath();
}

/**
 * Implements hook_theme_suggestions_media_oembed_iframe_alter().
 */
function varbase_media_theme_suggestions_media_oembed_iframe_alter(&$suggestions, &$vars) {

  // Suggestions go here.
  $query = \Drupal::request()->query;
  $type = $query
    ->get('type');
  $provider = $query
    ->get('provider');
  $view_mode = $query
    ->get('view_mode');
  if ($type && $provider) {
    $suggestions[] = "media_oembed_iframe__" . $provider;
    $suggestions[] = "media_oembed_iframe__" . $provider . "__" . $view_mode;
    $suggestions[] = "media_oembed_iframe__" . $view_mode;
    $suggestions[] = "media_oembed_iframe__" . $type;
    $suggestions[] = "media_oembed_iframe__" . $type . "__" . $view_mode;
    $suggestions[] = "media_oembed_iframe__" . $type . "__" . $provider;
    $suggestions[] = "media_oembed_iframe__" . $type . "__" . $provider . "__" . $view_mode;
  }
}

/**
 * Implements hook_page_attachments().
 */
function varbase_media_page_attachments(array &$page) {

  // Attached the varbase media common library.
  $page['#attached']['library'][] = 'varbase_media/common';
  if (!\Drupal::currentUser()
    ->isAnonymous()) {

    // Attached the varbase media common logged in users library.
    $page['#attached']['library'][] = 'varbase_media/common_logged';
  }
  if (Drupal::service('module_handler')
    ->moduleExists('blazy')) {

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

/**
 * Implements hook_ckeditor_css_alter().
 */
function varbase_media_ckeditor_css_alter(array &$css, Editor $editor) {

  // Varbase media path.
  $varbase_media_path = Drupal::service('module_handler')
    ->getModule('varbase_media')
    ->getPath();

  // Attached the varbase media common style.
  $css[] = $varbase_media_path . '/css/varbase_media.common.css';

  // Attached the varbase media common logged in users style.
  $css[] = $varbase_media_path . '/css/varbase_media.common_logged.css';
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
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":

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

        // 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 (Drupal::service('module_handler')
      ->moduleExists('blazy')) {

      // Attach the Blazy library.
      $form['#attached']['library'][] = 'blazy/load';
    }
    if (isset($form['revision_log_message'])) {
      $form['revision_log_message']['#access'] = false;
    }
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function varbase_media_theme_registry_alter(&$theme_registry) {

  // Varbase media path.
  $varbase_media_path = Drupal::service('module_handler')
    ->getModule('varbase_media')
    ->getPath();
  $theme_registry['entity_embed_container']['path'] = $varbase_media_path . '/templates';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function varbase_media_preprocess_entity_embed_container(&$variables) {
  $variables['url'] = isset($variables['element']['#context']['data-entity-embed-display-settings']['link_url']) ? UrlHelper::filterBadProtocol($variables['element']['#context']['data-entity-embed-display-settings']['link_url']) : '';
}

/**
 * Implements hook_entity_view_alter().
 */
function varbase_media_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  if ($entity
    ->getEntityTypeId() == 'media' && $build['#view_mode'] != 'field_preview') {
    if (isset($build['field_media_cover_image']) && isset($build['field_media_cover_image']['#items'])) {
      $fields = $build['field_media_cover_image']['#items'];
      if (is_object($fields)) {

        // Hide thumbnail of media if we do have cover image data.
        if (isset($build['thumbnail'])) {
          $build['thumbnail']['#access'] = FALSE;
        }
        $build['field_media_cover_image']['#attached']['library'][] = 'varbase_media/varbase_video_player';
      }
    }

    // Attach the varbase media video library for video embed field.
    if (isset($build['field_media_oembed_video']) && isset($build['field_media_oembed_video'][0])) {
      $build['field_media_oembed_video'][0]['#attached']['library'][] = 'varbase_media/varbase_video_player';
    }

    // Attach the varbase media video library for video file field.
    if (isset($build['field_media_video_file'])) {
      $build['field_media_video_file']['#attached']['library'][] = 'varbase_media/varbase_video_player';
    }
  }
}

/**
 * Implements hook_media_source_info_alter().
 */
function varbase_media_media_source_info_alter(array &$sources) {

  // File.
  // ---------------------------------------------------------------------------
  //  $sources['file']['input_match'] = [
  //    'constraint' => 'file',
  //    'field_types' => [
  //      'file',
  //    ],
  //  ];
  //  Override::pluginClass($sources['file'], VarbaseMediaFile::class);
  // Image.
  // ---------------------------------------------------------------------------
  //  $sources['image']['input_match'] = [
  //    'constraint' => 'image',
  //    'field_types' => [
  //      'image',
  //    ],
  //  ];
  //  Override::pluginClass($sources['image'], VarbaseMediaImage::class);
  // Audio file.
  // ---------------------------------------------------------------------------
  //  $sources['audio_file']['input_match'] = [
  //    'constraint' => 'file',
  //    'field_types' => [
  //      'file',
  //    ],
  //  ];
  //  Override::pluginClass($sources['audio_file'], VarbaseMediaAudioFile::class);
  // Video file.
  // ---------------------------------------------------------------------------
  //  $sources['video_file']['input_match'] = [
  //    'constraint' => 'file',
  //    'field_types' => [
  //      'file',
  //    ],
  //  ];
  //  Override::pluginClass($sources['video_file'], VarbaseMediaVideoFile::class);
  // Remote Video.
  // ---------------------------------------------------------------------------
  $sources['oembed:video']['input_match'] = [
    'constraint' => 'oembed_resource',
    'field_types' => [
      'link',
      'string',
      'string_long',
    ],
  ];
  $sources['oembed:video']['preview'] = TRUE;
  Override::pluginClass($sources['oembed:video'], VarbaseMediaRemoteVideo::class);
}

/**
 * Implements hook_form_alter().
 */
function varbase_media_form_alter(&$form, &$form_state, $form_id) {
  if ($form_state
    ->getFormObject() instanceof \Drupal\Core\Entity\EntityFormInterface) {
    $entity_type = $form_state
      ->getFormObject()
      ->getEntity()
      ->getEntityTypeId();

    // Only for media entity type.
    if ($entity_type == 'media') {

      // No revision information or revision log message.
      if (isset($form['revision_information'])) {
        $form['revision_information']['#access'] = FALSE;
      }
    }
  }
  if ($form_id == 'entity_browser_image_browser_form' || $form_id == 'entity_browser_media_browser_form' || $form_id == 'entity_browser_editor_media_browser_form') {
    $form['#attached']['library'][] = 'varbase_media/auto_fill_media_data';
  }
}

/**
 * Implements hook_token_info_alter().
 */
function varbase_media_token_info_alter(&$data) {
  $data['tokens']['media']['social_large'] = [
    'name' => t('Social Large'),
    'description' => t("Social Large (1200x630) image for the selected media type."),
    'module' => 'media',
    'type' => 'url',
  ];
  $data['tokens']['media']['social_medium'] = [
    'name' => t('Social Medium'),
    'description' => t("Social Medium (600x315) image for the selected media type."),
    'module' => 'media',
    'type' => 'url',
  ];
  $data['tokens']['media']['social_small'] = [
    'name' => t('Social Small'),
    'description' => t("Social Small (280x150) image for the selected media type."),
    'module' => 'media',
    'type' => 'url',
  ];
}

/**
 * Implements hook_tokens().
 */
function varbase_media_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $url_options = [
    'absolute' => TRUE,
  ];
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()
      ->getLanguage($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = LanguageInterface::LANGCODE_DEFAULT;
  }
  $replacements = [];
  if ($type === 'media' && !empty($data['media'])) {

    /** @var \Drupal\media\MediaInterface $media_entity */
    $media_entity = \Drupal::service('entity.repository')
      ->getTranslationFromContext($data['media'], $langcode, [
      'operation' => 'media_entity_tokens',
    ]);
    foreach ($tokens as $token_name => $original) {
      switch ($token_name) {

        // Social Large (1200x630) image for the selected media type.
        case 'social_large':
          $replacements[$original] = _varbase_media_image_url($media_entity, 'social_large');
          break;

        // Social Medium (600x315) image for the selected media type.
        case 'social_medium':
          $replacements[$original] = _varbase_media_image_url($media_entity, 'social_medium');
          break;

        // Social Small (280x150) image for the selected media type.
        case 'social_small':
          $replacements[$original] = _varbase_media_image_url($media_entity, 'social_small');
          break;
      }
    }
  }
  return $replacements;
}

/**
 * Get the URL with image style for a selected media entity.
 *
 */
function _varbase_media_image_url($media_entity, $style_name = NULL) {
  $image_field_name = _varbase_media_default_image_field_name($media_entity);
  if ($img_entity = $media_entity
    ->get($image_field_name)
    ->first()) {
    if ($file_entity = $img_entity
      ->get('entity')
      ->getTarget()) {
      if (!empty($style_name)) {
        return ImageStyle::load($style_name)
          ->buildUrl($file_entity
          ->get('uri')
          ->first()
          ->getString());
      }
      else {
        return file_create_url($file_entity
          ->get('uri')
          ->getString());
      }
    }
  }
  return NULL;
}

/**
 * Get the default image field name for any media entity types.
 */
function _varbase_media_default_image_field_name($media_entity) {

  // Media entities with a valied field media image data it will come first.
  if (isset($media_entity->field_media_image) && ($img_entity = $media_entity
    ->get('field_media_image')
    ->first())) {
    return 'field_media_image';
  }
  elseif (isset($media_entity->field_media_cover_image) && ($img_entity = $media_entity
    ->get('field_media_cover_image')
    ->first())) {
    return 'field_media_cover_image';
  }
  else {
    return 'thumbnail';
  }
}

/**
 * Implements hook_theme().
 */
function varbase_media_theme($existing, $type, $theme, $path) {
  return [
    'media_oembed_iframe__remote_video' => [
      'template' => 'media-oembed-iframe--remote-video',
    ],
  ];
}

/**
 * Implements hook_preprocess_HOOK().
 */
function varbase_media_preprocess_media_oembed_iframe__remote_video(&$variables) {

  // Send variables for the remote_video oembed iframe theme template.
  $query = \Drupal::request()->query;
  $variables['type'] = $query
    ->get('type');
  $variables['provider'] = $query
    ->get('provider');
  $variables['view_mode'] = $query
    ->get('view_mode');
  $variables['base_path'] = base_path();
  $variables['varbase_media_path'] = \Drupal::service('module_handler')
    ->getModule('varbase_media')
    ->getPath();
}