acquia_dam-1.0.0-rc1/src/Plugin/Field/FieldFormatter/EmbedCodeFormatter.php

src/Plugin/Field/FieldFormatter/EmbedCodeFormatter.php
<?php

declare(strict_types=1);

namespace Drupal\acquia_dam\Plugin\Field\FieldFormatter;

use Drupal\acquia_dam\EmbedCodeFactory;
use Drupal\acquia_dam\Plugin\media\Source\Asset;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\media\Entity\MediaType;
use Drupal\media\MediaInterface;

/**
 * Field formatter to render media assets with their embed code from the DAM.
 *
 * @FieldFormatter(
 *   id = "acquia_dam_embed_code",
 *   label = @Translation("Embed code"),
 *   field_types = {
 *    "acquia_dam_asset"
 *   },
 * )
 */
final class EmbedCodeFormatter extends FormatterBase {

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

    $media = $items->getEntity();
    assert($media instanceof MediaInterface);
    $embed = $media->getSource()->getMetadata($media, 'embeds');

    if ($embed === NULL) {
      return $elements;
    }
    $setting = $this->getSetting('embed_style');

    $elements[0] = [
      '#type' => 'container',
      '#theme_wrappers' => ['container__acquia_dam_asset'],
      'embed' => EmbedCodeFactory::renderAsset($setting, $media),
    ];

    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings(): array {
    return [
      'embed_style' => 'original',
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary(): array {
    $style = $this->getSetting('embed_style');
    foreach (EmbedCodeFactory::getSelectOptions() as $embeds) {
      foreach ($embeds as $embed_key => $embed_label) {
        if ($embed_key === $style) {
          return [sprintf("Embed style: %s", $embed_label)];
        }
      }
    }

    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state): array {
    $asset_type = $this->getAssetType($form);

    if (!$asset_type) {
      $this->messenger()->addWarning($this->t('No matching asset type found for asset reference field.'));
      return [];
    }

    $element['embed_style'] = [
      '#title' => $this->t('Embed style'),
      '#type' => 'select',
      '#default_value' => $this->getSetting('embed_style'),
      '#options' => EmbedCodeFactory::getSelectOptions($asset_type),
    ];

    return $element;
  }

  /**
   * Returns the DAM asset type from the media type.
   *
   * @param array $form
   *   Form array.
   *
   * @return string
   *   Asset type like "pdf", "video", etc.
   */
  protected function getAssetType(array $form): string {
    $media_type = $form['#bundle'] ?? '';
    $media_type = MediaType::load($media_type);
    return $media_type ? $media_type->getSource()->getDerivativeId() : '';
  }

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(FieldDefinitionInterface $field_definition): bool {
    if ($field_definition->getTargetEntityTypeId() !== 'media') {
      return FALSE;
    }

    if (parent::isApplicable($field_definition)) {
      $media_type = $field_definition->getTargetBundle();

      if ($media_type) {
        $media_type = MediaType::load($media_type);
        return $media_type && $media_type->getSource() instanceof Asset;
      }
    }
    return FALSE;
  }

}

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

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