Commit 1df57f7b authored by scott_earnest's avatar scott_earnest
Browse files

Issue #3293872: Support for Image Field types.

parent 7f5aee03
Loading
Loading
Loading
Loading
+43 −28
Original line number Diff line number Diff line
@@ -248,14 +248,8 @@ class MicromodalFieldFormatter extends FormatterBase {

    foreach ($items as $delta => $item) {

      // Use this for default oembed thumbnails.
      if ($item instanceof ImageItem) {
        $formatter_type = 'image';
        $image_fieldname = 'thumbnail';
      }
      // Use this for custom uploaded thumbnail.
      elseif ($item instanceof EntityReferenceItem) {
        // Get the media ID of the video.
      // Use this for all image thumbnails.
      if ($item instanceof EntityReferenceItem) {
        $formatter_type = 'image';
        $image_fieldname = $item->getFieldDefinition()->getName();
      }
@@ -299,22 +293,27 @@ class MicromodalFieldFormatter extends FormatterBase {
          // Implementation for the thumbnail field.
          if ($formatter_type === 'image' && !empty($image_fieldname)) {

            // Media ID of the thumbnail.
            $thumbnail_id = $media->getFields()[$image_fieldname]->getValue()[0]['target_id'];
            // The image field itself, file or entity reference.
            $image_field = $media->getFields()[$image_fieldname];

            // Use the image style setting to style the thumbnail.
            if (!empty($thumbnail_id)) {
            // Media or File ID of the thumbnail.
            $target_id = $image_field->getValue()[0]['target_id'];

              // Load the media for the thumbnail.
              $thumbnail_media = $this->entityTypeManager->getStorage('media')->load($thumbnail_id);
            // Use the image style setting to style the thumbnail.
            if (!empty($target_id)) {

              // Check the file ID and update as necessary.
              if (method_exists($thumbnail_media, 'get')) {
                $thumbnail_id = $thumbnail_media->get('thumbnail')->target_id;
              // Get the file id - easy if not entity reference.
              if ($item instanceof ImageItem) {
                $thumbnail_file_id = $target_id;
              }
              // Load the media for the thumbnail, if it's media reference.
              else {
                $thumbnail_media = $this->entityTypeManager->getStorage('media')->load($target_id);
                $thumbnail_file_id = $thumbnail_media->get('thumbnail')->target_id;
              }

              // Load the thumbnail file entity.
              $thumbnail_file = $this->entityTypeManager->getStorage('file')->load($thumbnail_id);
              $thumbnail_file = $this->entityTypeManager->getStorage('file')->load($thumbnail_file_id);

              // Make sure an image style has been set.
              if (!empty($this->getSetting('thumbnail_image_style'))) {
@@ -326,28 +325,41 @@ class MicromodalFieldFormatter extends FormatterBase {
                ];

                // Find the fieldname that holds the thumbnail image.
                if (method_exists($thumbnail_media, 'getFieldDefinitions')) {

                if (
                  isset($thumbnail_media)
                  && method_exists($thumbnail_media, 'getFieldDefinitions')
                ) {
                  foreach ($thumbnail_media->getFieldDefinitions() as $field) {
                    // Disregard BaseFieldDefinitions.
                    if ($field instanceof FieldConfig) {
                      // Once we've found the image field, grab that name.
                      if ($field->getType() === 'image') {
                        $thumbnail_image_fieldname = $field->getName();
                        break;
                      }
                    }
                  }

                }

              }

              // Add in alt and title tags.
              if (!empty($thumbnail_image_fieldname)) {

                // Initialize our attributes array.
              $attributes = [];

              // If the item is an image - this is easy.
              if ($item instanceof ImageItem) {
                $item_values = $item->getValue();
                if (!empty($item_values['alt'])) {
                  $attributes['alt'] = $item_values['alt'];
                }
                if (!empty($item_values['title'])) {
                  $attributes['title'] = $item_values['title'];
                }
              }

              // Else we hopefully have the image field name on the image media.
              elseif (!empty($thumbnail_image_fieldname)) {

                // Alt text.
                if (!empty($thumbnail_media->$thumbnail_image_fieldname->getValue()[0]['alt'])) {
                  $attributes['alt'] = $thumbnail_media->$thumbnail_image_fieldname->getValue()[0]['alt'];
@@ -358,13 +370,16 @@ class MicromodalFieldFormatter extends FormatterBase {
                  $attributes['title'] = $thumbnail_media->$thumbnail_image_fieldname->getValue()[0]['title'];
                }

                // If we have attributes add them to the thumbnail.
                if (!empty($attributes)) {
                  $render_thumbnail['#attributes'] = $attributes;
              }

              // If we have no attributes, lets at least set alt to the name.
              if (empty($attributes)) {
                $attributes['alt'] = $media->label();
              }

              // Attach attributes add them to the thumbnail.
              $render_thumbnail['#attributes'] = $attributes;

              // Render out the thumbnail - this is the linked item.
              $linked_item = render($render_thumbnail);