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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\media\MediaSourceBase implements ContainerFactoryPluginInterface, MediaSourceInterface
- class \Drupal\media_acquiadam\Plugin\media\Source\AcquiadamAsset
- class \Drupal\media\MediaSourceBase implements ContainerFactoryPluginInterface, MediaSourceInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\SourceView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AcquiadamAsset:: |
protected | property | Media: Acquia DAM asset image helper service. | |
AcquiadamAsset:: |
protected | property | Media: Acquia DAM Asset Media Factory service. | |
AcquiadamAsset:: |
protected | property | Media: Acquia DAM asset metadata helper service. | |
AcquiadamAsset:: |
protected | property | The asset that we're going to render details for. | |
AcquiadamAsset:: |
public | function |
Form constructor. Overrides MediaSourceBase:: |
|
AcquiadamAsset:: |
public static | function |
Creates an instance of the plugin. Overrides MediaSourceBase:: |
|
AcquiadamAsset:: |
protected | function |
Creates the source field storage definition. Overrides MediaSourceBase:: |
|
AcquiadamAsset:: |
public | function |
Gets default configuration for this plugin. Overrides MediaSourceBase:: |
|
AcquiadamAsset:: |
public | function |
Gets the metadata for the given entity. Overrides MediaSourceBase:: |
|
AcquiadamAsset:: |
public | function |
Gets a list of metadata attributes provided by this plugin. Overrides MediaSourceInterface:: |
|
AcquiadamAsset:: |
public | function |
Form submission handler. Overrides MediaSourceBase:: |
|
AcquiadamAsset:: |
public | function |
AcquiadamAsset constructor. Overrides MediaSourceBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MediaSourceBase:: |
protected | property | The config factory service. | |
MediaSourceBase:: |
protected | property | The entity field manager service. | |
MediaSourceBase:: |
protected | property | The entity type manager service. | |
MediaSourceBase:: |
protected | property | The field type plugin manager service. | |
MediaSourceBase:: |
protected | property | Plugin label. | |
MediaSourceBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
MediaSourceBase:: |
public | function |
Creates the source field definition for a type. Overrides MediaSourceInterface:: |
2 |
MediaSourceBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
MediaSourceBase:: |
public | function |
Get the source field definition for a media type. Overrides MediaSourceInterface:: |
|
MediaSourceBase:: |
protected | function | Determine the name of the source field. | 2 |
MediaSourceBase:: |
protected | function | Get the source field options for the media type form. | |
MediaSourceBase:: |
protected | function | Returns the source field storage definition. | |
MediaSourceBase:: |
public | function |
Get the primary value stored in the source field. Overrides MediaSourceInterface:: |
|
MediaSourceBase:: |
public | function |
Prepares the media type fields for this source in the form display. Overrides MediaSourceInterface:: |
3 |
MediaSourceBase:: |
public | function |
Prepares the media type fields for this source in the view display. Overrides MediaSourceInterface:: |
6 |
MediaSourceBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
MediaSourceBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
1 |
MediaSourceInterface:: |
constant | Default empty value for metadata fields. | ||
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |