0e8c1182fc4fcb154a3034940101fcfd36ada435
[yaffs-website] / web / modules / contrib / crop / src / Plugin / Crop / EntityProvider / Media.php
1 <?php
2
3 namespace Drupal\crop\Plugin\Crop\EntityProvider;
4
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;
10
11 /**
12  * Media crop integration.
13  *
14  * @CropEntityProvider(
15  *   entity_type = "media",
16  *   label = @Translation("Media"),
17  *   description = @Translation("Provides crop integration for Media.")
18  * )
19  */
20 class Media extends EntityProviderBase implements ContainerFactoryPluginInterface {
21
22   /**
23    * Entity type manager service.
24    *
25    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
26    */
27   protected $entityTypeManager;
28
29   /**
30    * Constructs media integration plugin.
31    *
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.
40    */
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;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
50     return new static(
51       $configuration,
52       $plugin_id,
53       $plugin_definition,
54       $container->get('entity_type.manager')
55     );
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function uri(EntityInterface $entity) {
62
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());
66
67     $image_field = $entity_type->getThirdPartySetting('crop', 'image_field');
68
69     if ($entity->{$image_field}->first()->isEmpty()) {
70       return FALSE;
71     }
72
73     /** @var \Drupal\file\FileInterface $image */
74     $image = $this->entityTypeManager->getStorage('file')->load($entity->{$image_field}->target_id);
75
76     if (strpos($image->getMimeType(), 'image') !== 0) {
77       return FALSE;
78     }
79
80     return $image->getFileUri();
81   }
82
83 }