You are here

class AcquiadamAsset in Media: Acquia DAM 8

Provides media type plugin for Acquia DAM assets.

Plugin annotation


@MediaSource(
  id = "acquiadam_asset",
  label = @Translation("Acquia DAM asset"),
  description = @Translation("Provides business logic and metadata for
  assets stored on Acquia DAM."), allowed_field_types = {"integer"},
)

Hierarchy

Expanded class hierarchy of AcquiadamAsset

3 files declare their use of AcquiadamAsset
AcquiadamAssetTest.php in tests/src/Unit/AcquiadamAssetTest.php
AcquiadamMediaTest.php in tests/src/Kernel/AcquiadamMediaTest.php
DAMAsset.php in src/Plugin/Linkit/Substitution/DAMAsset.php

File

src/Plugin/media/Source/AcquiadamAsset.php, line 28

Namespace

Drupal\media_acquiadam\Plugin\media\Source
View source
class AcquiadamAsset extends MediaSourceBase {

  /**
   * The asset that we're going to render details for.
   *
   * @var \cweagans\webdam\Entity\Asset
   */
  protected $currentAsset = NULL;

  /**
   * Media: Acquia DAM asset image helper service.
   *
   * @var \Drupal\media_acquiadam\Service\AssetImageHelper
   */
  protected $assetImageHelper;

  /**
   * Media: Acquia DAM asset metadata helper service.
   *
   * @var \Drupal\media_acquiadam\Service\AssetMetadataHelper
   */
  protected $assetMetadataHelper;

  /**
   * Media: Acquia DAM Asset Media Factory service.
   *
   * @var \Drupal\media_acquiadam\Service\AssetMediaFactory
   */
  protected $assetMediaFactory;

  /**
   * AcquiadamAsset constructor.
   *
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, AssetImageHelper $assetImageHelper, AssetMetadataHelper $assetMetadataHelper, AssetMediaFactory $assetMediaFactory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
    $this->assetImageHelper = $assetImageHelper;
    $this->assetMetadataHelper = $assetMetadataHelper;
    $this->assetMediaFactory = $assetMediaFactory;
  }

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

    // Fieldset with configuration options not needed.
    hide($form);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('entity_field.manager'), $container
      ->get('plugin.manager.field.field_type'), $container
      ->get('config.factory'), $container
      ->get('media_acquiadam.asset_image.helper'), $container
      ->get('media_acquiadam.asset_metadata.helper'), $container
      ->get('media_acquiadam.asset_media.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'source_field' => 'field_acquiadam_asset_id',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $submitted_config = array_intersect_key($form_state
      ->getValues(), $this->configuration);
    foreach ($submitted_config as $config_key => $config_value) {
      $this->configuration[$config_key] = $config_value;
    }

    // For consistency, always use the default source_field field name.
    $default_field_name = $this
      ->defaultConfiguration()['source_field'];

    // Check if it already exists so it can be used as a shared field.
    $storage = $this->entityTypeManager
      ->getStorage('field_storage_config');
    $existing_source_field = $storage
      ->load('media.' . $default_field_name);

    // Set or create the source field.
    if ($existing_source_field) {

      // If the default field already exists, return the default field name.
      $this->configuration['source_field'] = $default_field_name;
    }
    else {

      // Default source field name does not exist, so create a new one.
      $field_storage = $this
        ->createSourceFieldStorage();
      $field_storage
        ->save();
      $this->configuration['source_field'] = $field_storage
        ->getName();
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function createSourceFieldStorage() {
    $default_field_name = $this
      ->defaultConfiguration()['source_field'];

    // Create the field.
    return $this->entityTypeManager
      ->getStorage('field_storage_config')
      ->create([
      'entity_type' => 'media',
      'field_name' => $default_field_name,
      'type' => reset($this->pluginDefinition['allowed_field_types']),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getMetadataAttributes() {
    return $this->assetMetadataHelper
      ->getMetadataAttributeLabels();
  }

  /**
   * Gets the metadata for the given entity.
   *
   * @param \Drupal\media\MediaInterface $media
   *   The media entity to get metadata from.
   * @param string $name
   *   The metadata item to get the value of.
   *
   * @return mixed|null
   *   The metadata value or NULL if unset.
   */
  public function getMetadata(MediaInterface $media, $name) {
    if (empty($this->currentAsset)) {
      $asset = $this->assetMediaFactory
        ->get($media)
        ->getAsset();
      if (empty($asset)) {
        return NULL;
      }
      $this->currentAsset = $asset;
    }
    switch ($name) {
      case 'default_name':
        return parent::getMetadata($media, 'default_name');
      case 'thumbnail_uri':
        return $this->assetImageHelper
          ->getThumbnail($this->currentAsset, $this->assetMediaFactory
          ->get($media)
          ->getFile());
      case 'file':
        $file = $this->assetMediaFactory
          ->get($media)
          ->getFile();
        $is_file = !empty($file) && $file instanceof FileInterface;
        return $is_file ? $file
          ->id() : NULL;
      default:
        return $this->assetMetadataHelper
          ->getMetadataFromAsset($this->currentAsset, $name);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiadamAsset::$assetImageHelper protected property Media: Acquia DAM asset image helper service.
AcquiadamAsset::$assetMediaFactory protected property Media: Acquia DAM Asset Media Factory service.
AcquiadamAsset::$assetMetadataHelper protected property Media: Acquia DAM asset metadata helper service.
AcquiadamAsset::$currentAsset protected property The asset that we're going to render details for.
AcquiadamAsset::buildConfigurationForm public function Form constructor. Overrides MediaSourceBase::buildConfigurationForm
AcquiadamAsset::create public static function Creates an instance of the plugin. Overrides MediaSourceBase::create
AcquiadamAsset::createSourceFieldStorage protected function Creates the source field storage definition. Overrides MediaSourceBase::createSourceFieldStorage
AcquiadamAsset::defaultConfiguration public function Gets default configuration for this plugin. Overrides MediaSourceBase::defaultConfiguration
AcquiadamAsset::getMetadata public function Gets the metadata for the given entity. Overrides MediaSourceBase::getMetadata
AcquiadamAsset::getMetadataAttributes public function Gets a list of metadata attributes provided by this plugin. Overrides MediaSourceInterface::getMetadataAttributes
AcquiadamAsset::submitConfigurationForm public function Form submission handler. Overrides MediaSourceBase::submitConfigurationForm
AcquiadamAsset::__construct public function AcquiadamAsset constructor. Overrides MediaSourceBase::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MediaSourceBase::$configFactory protected property The config factory service.
MediaSourceBase::$entityFieldManager protected property The entity field manager service.
MediaSourceBase::$entityTypeManager protected property The entity type manager service.
MediaSourceBase::$fieldTypeManager protected property The field type plugin manager service.
MediaSourceBase::$label protected property Plugin label.
MediaSourceBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
MediaSourceBase::createSourceField public function Creates the source field definition for a type. Overrides MediaSourceInterface::createSourceField 2
MediaSourceBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
MediaSourceBase::getSourceFieldDefinition public function Get the source field definition for a media type. Overrides MediaSourceInterface::getSourceFieldDefinition
MediaSourceBase::getSourceFieldName protected function Determine the name of the source field. 2
MediaSourceBase::getSourceFieldOptions protected function Get the source field options for the media type form.
MediaSourceBase::getSourceFieldStorage protected function Returns the source field storage definition.
MediaSourceBase::getSourceFieldValue public function Get the primary value stored in the source field. Overrides MediaSourceInterface::getSourceFieldValue
MediaSourceBase::prepareFormDisplay public function Prepares the media type fields for this source in the form display. Overrides MediaSourceInterface::prepareFormDisplay 3
MediaSourceBase::prepareViewDisplay public function Prepares the media type fields for this source in the view display. Overrides MediaSourceInterface::prepareViewDisplay 6
MediaSourceBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
MediaSourceBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
MediaSourceInterface::METADATA_FIELD_EMPTY constant Default empty value for metadata fields.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
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.
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.