3 namespace Drupal\crop\Plugin\Crop\EntityProvider;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\crop\EntityProviderBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Media crop integration.
14 * @CropEntityProvider(
15 * entity_type = "media",
16 * label = @Translation("Media"),
17 * description = @Translation("Provides crop integration for Media.")
20 class Media extends EntityProviderBase implements ContainerFactoryPluginInterface {
23 * Entity type manager service.
25 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
27 protected $entityTypeManager;
30 * Constructs media integration plugin.
32 * @param array $configuration
33 * A configuration array containing information about the plugin instance.
34 * @param string $plugin_id
35 * The plugin_id for the plugin instance.
36 * @param mixed $plugin_definition
37 * The plugin implementation definition.
38 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
39 * Entity type manager service.
41 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
42 parent::__construct($configuration, $plugin_id, $plugin_definition);
43 $this->entityTypeManager = $entity_type_manager;
49 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
54 $container->get('entity_type.manager')
61 public function uri(EntityInterface $entity) {
63 $bundle_entity_type = $entity->getEntityType()->getBundleEntityType();
64 /** @var \Drupal\Core\Config\Entity\ConfigEntityBase $entity_type */
65 $entity_type = $this->entityTypeManager->getStorage($bundle_entity_type)->load($entity->bundle());
67 $image_field = $entity_type->getThirdPartySetting('crop', 'image_field');
69 if ($entity->{$image_field}->first()->isEmpty()) {
73 /** @var \Drupal\file\FileInterface $image */
74 $image = $this->entityTypeManager->getStorage('file')->load($entity->{$image_field}->target_id);
76 if (strpos($image->getMimeType(), 'image') !== 0) {
80 return $image->getFileUri();