cloudinary-8.x-1.x-dev/modules/cloudinary_video/src/Plugin/Field/FieldFormatter/CloudinaryVideoPlayer.php

modules/cloudinary_video/src/Plugin/Field/FieldFormatter/CloudinaryVideoPlayer.php
<?php

namespace Drupal\cloudinary_video\Plugin\Field\FieldFormatter;

use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Plugin\Field\FieldFormatter\FileVideoFormatter;

/**
 * Plugin implementation of the cloudinary video field formatter.
 *
 * @FieldFormatter(
 *   id = "cloudinary_video_player",
 *   label = @Translation("Cloudinary Video Player"),
 *   field_types = {
 *     "cloudinary_video"
 *   }
 * )
 */
class CloudinaryVideoPlayer extends FileVideoFormatter {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'responsive' => FALSE,
      'player_type' => 'auto',
      'width' => 854,
      'height' => 480,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    // Note that we inherit settings from FileVideoFormatter which is file-based
    // formatter, but we don't use file for cloudinary_video field type.
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function prepareView(array $entities_items) {
    // Nothing to do here as we don't have a file reference.
  }

  /**
   * List of player type options.
   *
   * @return array
   *   The options.
   */
  protected function getPlayerTypeOptions() {
    return [
      'html5' => $this->t('HTML 5 Player'),
      'cloudinary' => $this->t('Cloudinary Video player'),
      'auto' => $this->t('Auto (defined in the module settings)'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form, $form_state);
    $element['multiple_file_display_type']['#access'] = FALSE;

    $element['player_type'] = [
      '#title' => $this->t('Player type'),
      '#type' => 'radios',
      '#weight' => -1,
      '#options' => $this->getPlayerTypeOptions(),
      '#default_value' => $this->getSetting('player_type'),
      '#description' => $this->t('Choose a player to play video with.'),
      '#required' => TRUE,
    ];

    $element['responsive'] = [
      '#title' => $this->t('Responsive Video'),
      '#type' => 'checkbox',
      '#weight' => 0,
      '#default_value' => $this->getSetting('responsive'),
      '#description' => $this->t("Make the video fill the width of it's container, adjusting to the size of the user's screen."),
    ];

    $field_name = $this->fieldDefinition->getName();
    $states = [
      'required' => [
        ':input[name="fields[' . $field_name . '][settings_edit_form][settings][responsive]"]' => ['checked' => FALSE],
      ],
      'disabled' => [
        ':input[name="fields[' . $field_name . '][settings_edit_form][settings][responsive]"]' => ['checked' => TRUE],
      ],
    ];

    $element['width']['#required'] = FALSE;
    $element['height']['#required'] = FALSE;
    $element['width']['#states'] = $states;
    $element['height']['#states'] = $states;

    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();

    // Get rid of size summary when responsive option is enabled.
    if ($this->getSetting('responsive')) {
      array_pop($summary);
    }

    $options = $this->getPlayerTypeOptions();
    $player_type = $this->getSetting('player_type');

    $summary[] = $this->t('Player type: %type', ['%type' => $options[$player_type]]);
    $summary[] = $this->t('Responsive: %responsive', [
      '%responsive' => $this->getSetting('responsive') ? $this->t('yes') : $this->t('no'),
    ]);

    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];

    $element = [
      '#type' => 'cloudinary_video',
      '#player_type' => $this->getSetting('player_type'),
      '#responsive' => $this->getSetting('responsive'),
      '#muted' => $this->getSetting('muted'),
      '#controls' => $this->getSetting('controls'),
      '#autoplay' => $this->getSetting('autoplay'),
      '#loop' => $this->getSetting('loop'),
    ];

    if (!$element['#responsive']) {
      $element['#width'] = $this->getSetting('width');
      $element['#height'] = $this->getSetting('height');
    }

    foreach ($items as $delta => $item) {
      $instance_id = 'cloudinary-video-' . Html::cleanCssIdentifier($item->public_id);

      $elements[$delta] = $element + [
        '#public_id' => $item->public_id,
        '#delivery_type' => $item->delivery_type,
        '#resource_type' => $item->resource_type,
        '#raw_transformation' => $item->transformation,
        '#attributes' => [
          'id' => $instance_id,
          'name' => $instance_id,
          'title' => $item->description,
        ],
      ];
    }

    return $elements;
  }

}

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

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