Skip to content

Instantly share code, notes, and snippets.

@DuaelFr
Created March 26, 2020 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DuaelFr/257f03f95aaba7b0a4637c44d5869c31 to your computer and use it in GitHub Desktop.
Save DuaelFr/257f03f95aaba7b0a4637c44d5869c31 to your computer and use it in GitHub Desktop.
<?php
/**
* Implements hook_preprocess_HOOK().
*
* Determines the image style to use according to the paragraph parent.
*/
function my_module_preprocess_paragraph(&$variables) {
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
$paragraph = $variables['elements']['#paragraph'];
if (
$paragraph->bundle() !== 'media'
|| empty($paragraph->field_paragraph_media->entity)
|| $paragraph->field_paragraph_media->entity->bundle() !== 'media_image'
) {
return;
}
/** @var \Drupal\Core\Entity\ContentEntityBase $parent */
$parent = $paragraph->getParentEntity();
// Default image style.
$respImageStyle = 'edit_content_full';
if ($parent->getEntityTypeId() === 'paragraph') {
// 3 columns paragraphs has even columns so we just need one style.
if ($parent->bundle() == 'three_columns') {
$respImageStyle = 'edit_content_third';
}
elseif ($parent->bundle() == 'two_columns') {
// 2 columns paragraphs with 50/50 settings have even columns too.
if ($parent->field_column_size->value == 'half') {
$respImageStyle = 'edit_content_half';
}
// For 33/66 and 66/33 settings we need to find in which column the paragraph
// has been placed.
else {
// The field_elements_double field can only contain one paragraph so if it's
// not ours, it's in the other one.
$firstColumn = $parent->field_elements_double->target_id == $paragraph->id();
if ($firstColumn xor $parent->field_column_size->value == 'last-third') {
$respImageStyle = 'edit_content_third';
}
else {
$respImageStyle = 'edit_content_2thirds';
}
}
}
}
// Set the image style name to the entity carrying the field so it can be handled by
// \Drupal\my_module\Plugin\Field\FieldFormatter\ResponsiveImageFormatter.
$paragraph->field_paragraph_media->entity->forced_image_style = $respImageStyle;
}
<?php
namespace Drupal\my_module\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter as ResponsiveImageFormatterCore;
/**
* Responsive image formatter that allow to force the format.
*
* If the entity that carries the image has a "forced_image_style" value
* this is the one used in place of the image style selected in the formatter
* configuration.
*
* @see my_module_paragraph_preprocess_paragraph().
*
* @FieldFormatter(
* id = "my_module_responsive_image",
* label = @Translation("Responsive image (forced by parent)"),
* field_types = {
* "image",
* },
* quickedit = {
* "editor" = "image"
* }
* )
*/
class ResponsiveImageFormatter extends ResponsiveImageFormatterCore implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$entity = $items->getEntity();
if (!empty($entity->forced_image_style)) {
$this->setSetting('responsive_image_style', $entity->forced_image_style);
}
return parent::viewElements($items, $langcode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment