You are here

media_directories_editor.module in Media Directories 8

Main module file.

File

modules/media_directories_editor/media_directories_editor.module
View source
<?php

/**
 * @file
 * Main module file.
 */
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Adding settings to the media_directories config form here.
 */
function media_directories_editor_form_media_directories_config_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['media_directories_editor'] = [
    '#type' => 'details',
    '#title' => t('Editor Settings'),
    '#description' => t("Here you'll find settings regarding the CKEditor integration."),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $form['media_directories_editor']['directories_button'] = [
    '#type' => 'fieldset',
    '#title' => t('Directories button'),
    '#description' => t('The desired text formats should be given the editor button. You can edit them <a href=":url">here</a>. <em>Make sure to also enable the embed media filter!</em>', [
      ':url' => Url::fromRoute('filter.admin_overview')
        ->toString(),
    ]),
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function media_directories_editor_form_entity_embed_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {
  $storage = $form_state
    ->getStorage();

  /** @var \Drupal\media\Entity\Media $entity */
  $entity = $storage['entity'];

  /** @var \Drupal\embed\Entity\EmbedButton $embed_button */
  $embed_button = $storage['embed_button'];
  if ($entity !== NULL) {

    /** @var \Drupal\file\Entity\File $file */
    $file = $entity
      ->get('thumbnail')->entity;
    $image_style = ImageStyle::load('thumbnail');
    if ($image_style !== NULL) {
      $image_style_url = $image_style
        ->buildUri($file
        ->getFileUri());
      $url = file_create_url($image_style_url);
      $image_style
        ->createDerivative($file
        ->getFileUri(), $image_style_url);

      // TODO it should be converted to render array.
      $form['entity']['#markup'] = '<a href="' . $entity
        ->toUrl()
        ->toString() . '" title="' . $entity
        ->label() . '"><img src="' . $url . '" alt="' . $entity
        ->label() . '" /></a>';
    }
  }
  if (isset($form['attributes']['data-entity-embed-display-settings']) && $entity
    ->bundle() === 'image') {
    $entity_element = $form_state
      ->get('entity_element');
    if (isset($entity_element['data-entity-embed-display-settings']) && !is_array($entity_element['data-entity-embed-display-settings'])) {
      $entity_element['data-entity-embed-display-settings'] = Json::decode($entity_element['data-entity-embed-display-settings']);
    }
  }

  // Add bundle validation constraint to entity browser.
  if (isset($form['entity_browser'])) {
    $bundles = $embed_button
      ->getTypeSetting('bundles');
    $bundles = array_combine($bundles, $bundles);
    $form['entity_browser']['#entity_browser_validators']['target_bundles'] = [
      'bundle' => $bundles,
    ];
  }
}

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

  // Remove some attributes we don't need.
  unset($variables['attributes']['data-entity-embed-display-settings']);
  unset($variables['attributes']['data-entity-embed-display']);
  unset($variables['attributes']['data-embed-button']);
}