vimeo_player-1.0.0-rc1/src/Element/VimeoPlayer.php

src/Element/VimeoPlayer.php
<?php

namespace Drupal\vimeo_player\Element;

use Drupal\Core\Render\Attribute\RenderElement;
use Drupal\Core\Render\Element\RenderElementBase;

/**
 * Provides a render element for a Vimeo video player with integraton with the Player SDK.
 */
#[RenderElement('vimeo_player')]
class VimeoPlayer extends RenderElementBase {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = static::class;
    return [
      '#pre_render' => [
        [$class, 'preRender'],
      ],
      '#options' => [],
      '#events_to_fire' => [],
      '#attributes' => [],
      '#theme' => 'vimeo_player',
      '#attached' => [
        'library' => ['vimeo_player/vimeo'],
      ],
    ];
  }

  public static function preRender($element) {
    if (!isset($element['#attributes']['class'])) {
      $element['#attributes']['class'] = [];
    }
    $element['#attributes']['class'][] = 'vimeo-player-wrapper';
    if (is_array($element['#options']['url'])) {
      if (!isset($element['#options']['url']['landscape']) || !isset($element['#options']['url']['portrait'])) {
        throw new \Exception(t('Both landscape and portrait URLs of the Vimeo Video to be specified.'));
      }
    }
    $element['#attributes']['data-vimeo-options'] = json_encode($element['#options']);
    if (isset($element['#events_to_fire'])) {
      $element['#attributes']['data-vimeo-events'] = json_encode($element['#events_to_fire']);
    }

    if (isset($element['#media_query'])) {
      $element['#attributes']['data-media-query'] = $element['#media_query'];
    }
    return $element;
  }
}

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

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