layoutcomponents-8.x-1.14-beta2/modules/lc_simple_video/lc_simple_video.module

modules/lc_simple_video/lc_simple_video.module
<?php

/**
 * @file
 * LC Simple video module file.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\file\Entity\File;

/**
 * Implements hook_help().
 */
function lc_simple_video_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Create help page.
    case 'help.page.lc_simple_video':
      $module_handler = \Drupal::service('module_handler');
      $module_path = $module_handler->getModule('lc_simple_video')->getPath();
      $file = $module_path . '/README.md';
      if (!file_exists($file)) {
        return '';
      }

      // Get content from file.
      $reader = file_get_contents($file);

      // Return "clean" content.
      return preg_replace("/\r\n|\n|\r/", "<br>", $reader);
  }
}

/**
 * Implements hook_theme().
 */
function lc_simple_video_theme($existing, $type, $theme, $path) {
  return [
    'layoutcomponents_block_content__simple_video' => [
      'base hook' => 'block',
    ],
  ];
}

/**
 * Implements hook_page_attachments().
 */
function lc_simple_video_page_attachments(&$page) {
  $page['#attached']['library'][] = 'lc_simple_video/lc_simple_video';
}

/**
 * Implements hook_preprocess_field().
 */
function lc_simple_video_preprocess_field(&$variables) {
  if ($variables['field_name'] == 'field_sv_upload_video' || $variables['field_name'] == 'field_sv_video') {

    /** @var \Drupal\block_content\Entity\BlockContent $block */
    $block = $variables['element']["#object"];
    $block_id = str_replace(' ', '_', $block->uuid());

    foreach ($variables['items'] as $i => $item) {
      // Values.
      $video_type = $block->get('field_sv_video_type')->getString();
      $video_ratio = $block->get('field_sv_video_ratio')->getString();
      $video_veil = $block->get('field_sv_video_veil')->getString();
      $extra_class = $block->get('field_sv_video_extra_class')->getString();
      $extra_attributes = $block->get('field_sv_video_extra_attributes')->getString();

      // Default styles.
      $styles = [];
      $classes = [
        'lc-inline_block_' . $block_id . '-video-edit', 'embed-responsive-item',
      ];
      $container_classes = [
        'lc-inline_block_' . $block_id . '-video-container-edit',
        'pl-0',
        'pr-0',
        'd-flex',
        'embed-responsive',
      ];

      if (!empty($video_ratio)) {
        $container_classes[] = 'embed-responsive-' . $video_ratio;
      }

      if (!empty($video_veil)) {
        $container_classes[] = 'lc-video-bg';
      }

      // Set classes.
      if (!empty($extra_class)) {
        $extra_class = explode(" ", $extra_class);
        $classes = array_merge($classes, $extra_class);
      }

      // Set attributes.
      $ex_attributes = [];
      if (!empty($extra_attributes)) {
        $parts = explode(" ", $extra_attributes);
        foreach ($parts as $attribute) {
          if (strpos($attribute, '|') !== FALSE) {
            list($key, $value) = explode('|', $attribute);
            $ex_attributes[$key] = $value;
          }
        }
      }

      // Set new classes and styles.
      $attributes = [
        'class' => $classes,
        'style' => implode($styles),
      ];

      $attributes = array_merge($attributes, $ex_attributes);

      $container_attributes = [
        'class' => $container_classes,
      ];

      $content = $variables['items'][$i]['content'];

      if ($video_type == 'video_url') {
        $content['children']['#attributes'] = $attributes;
        unset($content['children']['#attributes']['width']);
        unset($content['children']['#attributes']['height']);

        // Create image container.
        $container = [
          '#type' => "container",
          '#attributes' => $container_attributes,
          'content' => $content['children'],
          '#attached' => $content['#attached'],
        ];
      }

      if ($video_type == 'video_upload') {
        $attributes['controls'] = 'controls';
        // Set new attributes.
        $content['#attributes'] = $attributes;

        // Render the video directly.
        $content['#theme'] = 'file_video';

        /** @var \Drupal\media\Entity\Media $media */
        $media = $content['#media'];
        $file_id = $media->field_media_video_file->target_id;
        $file = File::load($file_id);

        // Set source attributes.
        $content['#files'][] = [
          'source_attributes' => 'src=' . $file->createFileUrl() . ' type=' . $file->getMimeType(),
        ];

        // Include the content in a new container.
        $container = [
          '#type' => "container",
          '#attributes' => $container_attributes,
          'content' => $content,
        ];
      }
      // Set the new element.
      $variables['items'][$i]['content'] = $container;
    }
  }
}

/**
 * Implements hook_inline_entity_form_entity_form_alter().
 */
function lc_simple_video_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
  if ($entity_form['#bundle'] == 'simple_video') {
    if (!array_key_exists('#default_value', $entity_form) || !isset($entity_form['#default_value'])) {
      return;
    }
    /** @var \Drupal\block_content\Entity\BlockContent $block */
    $block = $entity_form['#default_value'];
    $block_id = str_replace(" ", "_", $block->uuid());
    _lc_simple_video_form_alter($entity_form, $block_id);
  }
}

/**
 * Implements hook_block_type_form_alter().
 */
function lc_simple_video_block_type_form_alter(array &$form, FormStateInterface &$form_state, $block_type) {
  if ($block_type == "simple_video") {
    if (!array_key_exists('#block', $form)) {
      return;
    }
    /** @var \Drupal\block_content\Entity\BlockContent $block */
    $block = $form['#block'];
    $block_id = str_replace(" ", "_", $block->uuid());
    _lc_simple_video_form_alter($form, $block_id);
  }
}

/**
 * Change the elements with LayoutComponents Api.
 *
 * @param array $form
 *   The array with the form.
 * @param string $block_id
 *   The id of the block.
 */
function _lc_simple_video_form_alter(array &$form, $block_id) {
  /** @var \Drupal\layoutcomponents\Api\Component $lcApi */
  $lcApi = Drupal::service('layoutcomponents.apiComponent');

  // LC inline video type.
  $type = $form['field_sv_video_type']['widget'];
  $form['field_sv_video_type']['widget'] = $lcApi->getComponentElement(
    [
      'id' => 'block_' . $block_id . '-video',
      'no_lc' => TRUE,
    ],
    $type
  );

  // LC inline video url.
  $url = $form['field_sv_video']['widget'][0]['value'];
  $form['field_sv_video']['widget'][0]['value'] = $lcApi->getComponentElement(
    [
      'id' => 'block_' . $block_id . '-video',
      'no_lc' => TRUE,
    ],
    $url
  );

  $form['field_sv_video']['widget'][0]['value']['#states'] = [
    'visible' => [
      ':input[name="settings[block_form][field_sv_video_type]"]' => ['value' => 'video_url'],
    ],
    'invisible' => [
      ':input[name="settings[block_form][field_sv_video_type]"]' => ['!value' => 'video_url'],
    ],
  ];

  // LC inline video upload.
  $upload = $form['field_sv_upload_video']['widget'];
  $form['field_sv_upload_video']['widget'] = $lcApi->getComponentElement(
    [
      'id' => 'block_' . $block_id . '-video',
      'no_lc' => TRUE,
    ],
    $upload
  );

  $form['field_sv_upload_video']['widget']['#states'] = [
    'visible' => [
      ':input[name="settings[block_form][field_sv_video_type]"]' => ['value' => 'video_upload'],
    ],
    'invisible' => [
      ':input[name="settings[block_form][field_sv_video_type]"]' => ['!value' => 'video_upload'],
    ],
  ];

  // LC inline video ratio.
  $ratio = $form['field_sv_video_ratio']['widget'];
  $form['field_sv_video_ratio']['widget'] = $lcApi->getComponentElement(
    [
      'id' => 'block_' . $block_id . '-video-container',
      'type' => 'class',
      'style' => 'ratio',
      'class_remove' => 'embed-responsive-*',
      'element' => 'select',
      'class' => 'ratio',
    ],
    $ratio
  );

  // LC inline video extra class.
  $extra_class = $form['field_sv_video_extra_class']['widget'][0]['value'];
  $form['field_sv_video_extra_class']['widget'][0]['value'] = $lcApi->getComponentElement(
    [
      'id' => 'block_' . $block_id . '-video',
      'input' => 'text',
      'type' => 'class',
      'style' => 'extra_class',
      'element' => 'text',
    ],
    $extra_class
  );

  // LC extra attributes.
  $extra_attributes = &$form['field_sv_video_extra_attributes']['widget'][0]['value'];
  $form['field_sv_video_extra_attributes']['widget'][0]['value'] = $lcApi->getComponentElement(
    [
      'id' => 'block_' . $block_id . '-video',
      'input' => 'text',
      'type' => 'attribute',
      'style' => 'extra_attributes',
      'element' => 'text',
    ],
    $extra_attributes
  );

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc