You are here

class DAMAsset in Media: Acquia DAM 8

A substitution plugin for the URL to a file.

Custom plugin for DAM assets because the source field is the DAM ID, not the actual file reference field.

Plugin annotation


@Substitution(
  id = "dam_asset",
  label = @Translation("Direct URL to DAM file entity"),
)

Hierarchy

Expanded class hierarchy of DAMAsset

File

src/Plugin/Linkit/Substitution/DAMAsset.php, line 25

Namespace

Drupal\media_acquiadam\Plugin\Linkit\Substitution
View source
class DAMAsset extends Media {

  /**
   * Drupal entity type management service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@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'));
  }

  /**
   * {@inheritdoc}
   */
  public function getUrl(EntityInterface $entity) {

    // We need special handling for Acquia DAM media sources.
    // LinkIt assumes that a Media source field will be a FileInterface which is
    // not a valid assumption.
    if ($entity instanceof MediaInterface) {
      $source = $entity
        ->getSource();
      if (!empty($source) && $source instanceof AcquiadamAsset) {
        $fid = $source
          ->getMetadata($entity, 'file');
        if (!empty($fid)) {
          $file = $this->entityTypeManager
            ->getStorage('file')
            ->load($fid);

          // This is the original LinkIt behavior.
          if (!empty($file) && $file instanceof FileInterface) {
            $url = new GeneratedUrl();
            $url
              ->setGeneratedUrl(file_create_url($file
              ->getFileUri()));
            $url
              ->addCacheableDependency($entity);
            return $url;
          }
        }
      }
    }
    return parent::getUrl($entity);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DAMAsset::$entityTypeManager protected property Drupal entity type management service.
DAMAsset::create public static function Creates an instance of the plugin. Overrides Media::create
DAMAsset::getUrl public function Get the URL associated with a given entity. Overrides Media::getUrl
DAMAsset::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
Media::isApplicable public static function Checks if this substitution plugin is applicable for the given entity type. Overrides SubstitutionInterface::isApplicable
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.