Skip to content
Snippets Groups Projects

Issue #3338656: Allow to choose media view mode for layout background image

@@ -28,6 +28,12 @@ abstract class RocketshipCoreBaseLayout extends LayoutDefault implements PluginF
*/
protected $entityTypeManager;
/**
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* {@inheritdoc}
*/
@@ -35,6 +41,7 @@ abstract class RocketshipCoreBaseLayout extends LayoutDefault implements PluginF
$instance = new static($configuration, $plugin_id, $plugin_definition);
$instance->currentUser = $container->get('current_user');
$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->entityDisplayRepository = $container->get('entity_display.repository');
return $instance;
}
@@ -176,6 +183,20 @@ abstract class RocketshipCoreBaseLayout extends LayoutDefault implements PluginF
'#default_value' => $background_image_default,
];
$form['background_wrapper']['background_image_view_mode'] = [
'#type' => 'select',
'#title' => $this->t('Background image view mode'),
'#description' => $this->t('Select view mode of media entity to be used.'),
'#options' => [
'layout_builder_background' => 'layout_builder_background'
],
'#default_value' => $this->configuration['background_image_view_mode']
];
$view_modes = $this->entityDisplayRepository->getViewModeOptionsByBundle('media', 'image');
foreach($view_modes as $view_mode => $label) {
$form['background_wrapper']['background_image_view_mode']['#options'][$view_mode] = $label;
}
return $form;
}
@@ -220,6 +241,12 @@ abstract class RocketshipCoreBaseLayout extends LayoutDefault implements PluginF
}
}
$this->configuration['background_image'] = $image;
$this->configuration['background_image_view_mode'] = $form_state->getValue([
'background_wrapper',
'background_image_view_mode'
], 'layout_builder_background');
$color = $form_state->getValue([
'background_wrapper',
'background_color',
@@ -275,10 +302,11 @@ abstract class RocketshipCoreBaseLayout extends LayoutDefault implements PluginF
$media = $this->entityTypeManager->getStorage('media')
->loadByProperties(['uuid' => $background_image_uuid]);
if ($media) {
$background_image_view_mode = $this->configuration['background_image_view_mode'] ?? 'layout_builder_background';
$media = reset($media);
$view_mode = $this->entityTypeManager
->getViewBuilder('media')
->view($media, 'layout_builder_background');
->view($media, $background_image_view_mode);
$build['layout_background_image'] = $view_mode;
}
// print with {{ content.layout_background_image }}
Loading