MediaFinderBase.php in Group Media 8.2
File
src/Plugin/MediaFinder/MediaFinderBase.php
View source
<?php
namespace Drupal\groupmedia\Plugin\MediaFinder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\groupmedia\MediaFinderInterface;
use Drupal\media\MediaInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class MediaFinderBase extends PluginBase implements MediaFinderInterface, ContainerFactoryPluginInterface {
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $moduleHandler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $moduleHandler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'));
}
public function getApplicableFieldTypes() {
return $this->pluginDefinition['field_types'] ?: [];
}
public function applies(EntityInterface $entity) {
if ($entity instanceof FieldableEntityInterface) {
return TRUE;
}
return FALSE;
}
public function shouldBeAdded(MediaInterface $media, EntityInterface $entity, $field_name) {
$return = [];
$context = [
'entity' => $entity,
'field_name' => $field_name,
];
$this->moduleHandler
->alter('groupmedia_finder_add', $return, $media, $context);
if (!is_array($return)) {
return FALSE;
}
foreach ($return as $item) {
if (!$item) {
return FALSE;
}
}
return TRUE;
}
}