Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
<?php

namespace Drupal\atoms_media_library\Plugin\Atoms;

use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Config\Config;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\atoms\Atom;
use Drupal\atoms\AtomsPluginBase;
use Drupal\atoms\ViewableAtom;

/**
 * @Atoms(
 *  id = "media",
 *  title = "Media",
 *  description = "Media",
 *  types = {
 *    "media"
 *  }
 * )
 */
class Media extends AtomsPluginBase {

  /**
   * @inheritDoc
   */
  public function getTypeNames() {
    return [
      'media' => 'Media',
    ];
  }

  /**
   * @inheritDoc
   */
  public function hasTokenSupport() {
    return FALSE;
  }

  /**
   * @inheritDoc
   */
  public function formBuilder(Atom $atom) {
    $entity_type = 'media';
    $entities = $this->value($atom->view());

    $form = [
      '#type' => 'media_library',
      '#title' => $this->t($atom->getTitle()),
      '#description' => $this->t($atom->getDescription()),
      '#target_type' => $entity_type,
      '#allowed_bundles' => ['image']
    ];

    if ($atom->getOptions()['multiple'] ?? FALSE) {
      $form['#tags'] = TRUE;
      $form['#default_value'] = $entities;
    }
    else {
      $form['#default_value'] = empty($entities) ? NULL : reset($entities)->id();
    }

    if (!empty($atom->getOptions()['target_bundles'])) {
      $form['#allowed_bundles'] = $atom->getOptions()['target_bundles'];
    }

    return $form;
  }

  /**
   * @inheritDoc
   */
  public function submit(Atom $atom, FormStateInterface $form_state) {
    $value = $form_state->getValue($this->getFormStateKey($atom));
    $entity_ids = [];
    if (is_array($value)) {
      foreach ($value as $array) {
        $entity_ids[] = $array['target_id'];
      }
    }
    else {
      $entity_ids[] = intval($value);
    }
    $atom->setData($entity_ids);
  }

  /**
   * @inheritDoc
   */
  function renderBuild(ViewableAtom $view, array &$build) {
    $entity_type = 'media';
    $entities = $this->value($view);
    foreach ($entities as $entity) {
      $this->bubbleable_metadata->addCacheableDependency($entity);
    }
    $view_mode = $view->getAtom()
        ->getOptions()['view_mode'] ?? ($this->getConfig()
          ->get($this->getPluginId() . '_view_mode') ?? 'full');

    $build += \Drupal::entityTypeManager()
      ->getViewBuilder($entity_type)
      ->viewMultiple($entities, $view_mode, $view->getLangCode());
  }

  /**
   * @inheritDoc
   */
  public function summary(Atom $atom) {
    $entity_type = 'media';
    $entities = $this->value($atom->view());
    $view_mode = $atom->getOptions()['summary_view_mode'] ?? ($this->getConfig()
          ->get($this->getPluginId() . '_summary_view_mode') ?? 'media_library');

    return \Drupal::entityTypeManager()
      ->getViewBuilder($entity_type)
      ->viewMultiple($entities, $view_mode, $atom->getLangCode());
  }

  /**
   * @inheritDoc
   */
  public function value(ViewableAtom $view, $key = '') {
    $entity_type = 'media';
    $entity_ids = $view->getData() ?? [];
    if (!is_array($entity_ids)) {
      $entity_ids = [$entity_ids];
    }
    try {
      $entities = \Drupal::entityTypeManager()
        ->getStorage($entity_type)
        ->loadMultiple($entity_ids);
    } catch (InvalidPluginDefinitionException $e) {
      \Drupal::logger('atoms')
        ->error('Atom (' . $view->getAtom()
            ->getMachineName() . ') of type ' . $view->getType() . ' caused an InvalidPluginDefinitionException');
      $entities = [];
    } catch (PluginNotFoundException $e) {
      \Drupal::logger('atoms')
        ->error('Atom (' . $view->getAtom()
            ->getMachineName() . ') of type ' . $view->getType() . ' caused an PluginNotFoundException');
      $entities = [];
    }
    return $entities;
  }

  /**
   * @inheritDoc
   */
  public function values(ViewableAtom $view, $key = '') {
    $entity_ids = $view->getData() ?? [];
    if (!is_array($entity_ids)) {
      $entity_ids = [$entity_ids];
    }
    return ['target_ids' => $entity_ids];
  }

  /**
   * @inheritDoc
   */
  public function settingsForm(Config $config, FormStateInterface $form_state) {
    // call the entity service.
    /** @var \Drupal\Core\Entity\EntityDisplayRepository $entityDisplay */
    $entityDisplay = \Drupal::service('entity_display.repository');

    // call the necessary method in order to return 'media' view modes.
    $viewModes = $entityDisplay->getViewModes('media');

    $options = [];
    /** @var DateFormat $format */
    foreach ($viewModes as $id => $viewMode) {
      $options[$id] = $viewMode['label'];
    }

    $form[$this->getPluginId() . '_view_mode'] = [
      '#type' => 'select',
      '#title' => $this->t('Default view mode'),
      '#options' => $options,
      '#default_value' => $config->get($this->getPluginId() . '_view_mode') ?: 'full',
    ];

    $form[$this->getPluginId() . '_summary_view_mode'] = [
      '#type' => 'select',
      '#title' => $this->t('Summary view mode'),
      '#options' => $options,
      '#default_value' => $config->get($this->getPluginId() . '_summary_view_mode') ?: 'media_library',
    ];

    return $form;
  }

  /**
   * @inheritDoc
   */
  public function settingsValidate(Config $config, FormStateInterface $form_state) {
    return TRUE;
  }

  /**
   * @inheritDoc
   */
  public function settingsSubmit(Config $config, FormStateInterface $form_state) {
    $config->set($this->getPluginId() . '_view_mode', $form_state->getValue($this->getPluginId() . '_view_mode'));
    $config->set($this->getPluginId() . '_summary_view_mode', $form_state->getValue($this->getPluginId() . '_summary_view_mode'));
  }

}