1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\crop_media_entity\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 entity crop integration. |
13
|
|
|
* |
14
|
|
|
* @CropEntityProvider( |
15
|
|
|
* entity_type = "media", |
16
|
|
|
* label = @Translation("Media"), |
17
|
|
|
* description = @Translation("Provides crop integration for media entity.") |
18
|
|
|
* ) |
19
|
|
|
*/ |
20
|
|
|
class MediaEntity 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 entity 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
|
|
|
/** @var \Drupal\media_entity\MediaBundleInterface $bundle */ |
63
|
|
|
$bundle = $this->entityTypeManager->getStorage('media_bundle')->load($entity->bundle()); |
64
|
|
|
$image_field = $bundle->getThirdPartySetting('crop', 'image_field'); |
65
|
|
|
|
66
|
|
|
if ($entity->{$image_field}->first()->isEmpty()) { |
67
|
|
|
return FALSE; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var \Drupal\file\FileInterface $image */ |
71
|
|
|
$image = $this->entityTypeManager->getStorage('file')->load($entity->{$image_field}->target_id); |
72
|
|
|
|
73
|
|
|
if (strpos($image->getMimeType(), 'image') !== 0) { |
74
|
|
|
return FALSE; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $image->getFileUri(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|