You are here

class Slideshow in Media entity slideshow 8

Provides media type plugin for Slideshows.

Plugin annotation


@MediaType(
  id = "slideshow",
  label = @Translation("Slideshow"),
  description = @Translation("Provides business logic and metadata for slideshows.")
)

Hierarchy

Expanded class hierarchy of Slideshow

File

src/Plugin/MediaEntity/Type/Slideshow.php, line 18

Namespace

Drupal\media_entity_slideshow\Plugin\MediaEntity\Type
View source
class Slideshow extends MediaTypeBase {

  /**
   * {@inheritdoc}
   */
  public function providedFields() {
    $fields = array(
      'length' => $this
        ->t('Slideshow length'),
    );
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getField(MediaInterface $media, $name) {
    $source_field = $this->configuration['source_field'];
    switch ($name) {
      case 'length':
        return $media->{$source_field}
          ->count();
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

    /** @var MediaBundleInterface $bundle */
    $bundle = $form_state
      ->getFormObject()
      ->getEntity();
    $options = [];
    $allowed_field_types = [
      'entity_reference',
    ];

    /** @var \Drupal\Core\Field\FieldDefinitionInterface $field */
    foreach ($this->entityFieldManager
      ->getFieldDefinitions('media', $bundle
      ->id()) as $field_name => $field) {
      if (in_array($field
        ->getType(), $allowed_field_types)) {
        $storage = $field
          ->getFieldStorageDefinition();
        if (!$storage
          ->isBaseField() && $storage
          ->getSetting('target_type') == 'media') {
          $options[$field_name] = $field
            ->getLabel();
        }
      }
    }
    $form['source_field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Field with source information'),
      '#description' => $this
        ->t('Field on media entity that stores slideshow items. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'),
      '#default_value' => empty($this->configuration['source_field']) ? NULL : $this->configuration['source_field'],
      '#options' => $options,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function attachConstraints(MediaInterface $media) {
    parent::attachConstraints($media);
    $source_field_name = $this->configuration['source_field'];

    // Validate slideshow items count.
    $media
      ->getTypedData()
      ->getDataDefinition()
      ->addConstraint('ItemsCount', array(
      'sourceFieldName' => $source_field_name,
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultThumbnail() {
    return $this->config
      ->get('icon_base') . '/slideshow.png';
  }

  /**
   * {@inheritdoc}
   */
  public function thumbnail(MediaInterface $media) {
    $source_field = $this->configuration['source_field'];

    /** @var \Drupal\media_entity\MediaInterface $slideshow_item */
    $slideshow_item = $this->entityTypeManager
      ->getStorage('media')
      ->load($media->{$source_field}->target_id);
    if (!$slideshow_item) {
      return $this
        ->getDefaultThumbnail();
    }

    /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
    $bundle = $this->entityTypeManager
      ->getStorage('media_bundle')
      ->load($slideshow_item
      ->bundle());
    if (!$bundle) {
      return $this
        ->getDefaultThumbnail();
    }
    $thumbnail = $bundle
      ->getType()
      ->thumbnail($slideshow_item);
    if (!$thumbnail) {
      return $this
        ->getDefaultThumbnail();
    }
    return $thumbnail;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultName(MediaInterface $media) {

    // The default name will be the timestamp + number of slides.
    $length = $this
      ->getField($media, 'length');
    if (!empty($length)) {
      return $this
        ->formatPlural($length, '1 slide, created on @date', '@count slides, created on @date', [
        '@date' => \Drupal::service('date.formatter')
          ->format($media
          ->getCreatedTime(), 'custom', 'd/M/Y - H:i:s'),
      ]);
    }
    return parent::getDefaultName($media);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaTypeBase::$config protected property Media entity image config object.
MediaTypeBase::$entityFieldManager protected property The entity field manager service.
MediaTypeBase::$entityTypeManager protected property The entity type manager service.
MediaTypeBase::$label protected property Plugin label.
MediaTypeBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
MediaTypeBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MediaTypeBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurablePluginInterface::defaultConfiguration 1
MediaTypeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
MediaTypeBase::label public function Returns the display label. Overrides MediaTypeInterface::label
MediaTypeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration
MediaTypeBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
MediaTypeBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
MediaTypeBase::__construct public function Constructs a new class instance. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
Slideshow::attachConstraints public function Attaches type-specific constraints to media. Overrides MediaTypeBase::attachConstraints
Slideshow::buildConfigurationForm public function Form constructor. Overrides MediaTypeBase::buildConfigurationForm
Slideshow::getDefaultName public function Provide a default name for the media. Overrides MediaTypeBase::getDefaultName
Slideshow::getDefaultThumbnail public function Gets the default thumbnail image. Overrides MediaTypeBase::getDefaultThumbnail
Slideshow::getField public function Gets a media-related field/value. Overrides MediaTypeInterface::getField
Slideshow::providedFields public function Gets list of fields provided by this plugin. Overrides MediaTypeInterface::providedFields
Slideshow::thumbnail public function Gets thumbnail image. Overrides MediaTypeInterface::thumbnail
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.