Skip to content

Instantly share code, notes, and snippets.

@phenaproxima
Last active May 29, 2019 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phenaproxima/319914a266d9663d93ad1aa86da47e3b to your computer and use it in GitHub Desktop.
Save phenaproxima/319914a266d9663d93ad1aa86da47e3b to your computer and use it in GitHub Desktop.
diff --git a/src/EntityEmbedDisplay/EntityEmbedDisplayManager.php b/src/EntityEmbedDisplay/EntityEmbedDisplayManager.php
index ce560df..ceb8605 100644
--- a/src/EntityEmbedDisplay/EntityEmbedDisplayManager.php
+++ b/src/EntityEmbedDisplay/EntityEmbedDisplayManager.php
@@ -5,8 +5,10 @@ namespace Drupal\entity_embed\EntityEmbedDisplay;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
+use Drupal\Core\Image\Image;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Component\Plugin\Exception\PluginException;
+use Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay\ImageAwareDecorator;
/**
* Provides an Entity Embed display plugin manager.
@@ -176,4 +178,14 @@ class EntityEmbedDisplayManager extends DefaultPluginManager {
}, $definitions);
}
+ public function createInstance($plugin_id, array $configuration = []) {
+ $configuration += [
+ '_entity_embed_image_decorator' => TRUE,
+ ];
+ $instance = parent::createInstance($plugin_id, $configuration);
+ return $configuration['_entity_embed_image_decorator']
+ ? new ImageAwareDecorator($instance)
+ : $instance;
+ }
+
}
diff --git a/src/Plugin/entity_embed/EntityEmbedDisplay/ImageAwareDecorator.php b/src/Plugin/entity_embed/EntityEmbedDisplay/ImageAwareDecorator.php
new file mode 100644
index 0000000..f714662
--- /dev/null
+++ b/src/Plugin/entity_embed/EntityEmbedDisplay/ImageAwareDecorator.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay;
+
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Session\AccountInterface;
+use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface;
+
+class ImageAwareDecorator implements EntityEmbedDisplayInterface {
+
+ /**
+ * @var EntityEmbedDisplayInterface
+ */
+ private $decorated;
+
+ public function __construct(EntityEmbedDisplayInterface $decorated) {
+ $this->decorated = $decorated;
+ }
+
+ public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
+ $form = $this->decorated->buildConfigurationForm($form, $form_state);
+
+ if ($this->isEmbeddingAnImage()) {
+ // Add the alt and title fields.
+ }
+ return $form;
+ }
+
+ public function access(AccountInterface $account = NULL) {
+ return $this->decorated->access($account);
+ }
+
+ public function calculateDependencies() {
+ return $this->decorated->calculateDependencies();
+ }
+
+ public function getConfiguration() {
+ return $this->decorated->getConfiguration();
+ }
+
+ public function build() {
+ $build = $this->decorated->build();
+
+ if ($this->isEmbeddingAnImage()) {
+ // Add the alt and title if needed.
+ }
+ return $build;
+ }
+
+ public function getPluginDefinition() {
+ return $this->decorated->getPluginDefinition();
+ }
+
+ public function getPluginId() {
+ return $this->decorated->getPluginId();
+ }
+
+ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
+ $this->decorated->validateConfigurationForm($form, $form_state);
+
+ if ($this->isEmbeddingAnImage()) {
+ // Validate the alt and title too.
+ }
+ }
+
+ public function defaultConfiguration() {
+ return $this->decorated->defaultConfiguration();
+ }
+
+ public function setConfiguration(array $configuration) {
+ return $this->decorated->setConfiguration($configuration);
+ }
+
+ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
+ $this->decorated->submitConfigurationForm($form, $form_state);
+
+ if ($this->isEmbeddingAnImage()) {
+ // Also store the alt and title text as needed.
+ }
+ }
+
+ private function isEmbeddingAnImage() {
+ // Return TRUE if the entity being embedded is some sort of image (e.g.,
+ // a file with MIME type image/*, or media item wrapping one), FALSE
+ // otherwise.
+ }
+
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment