Skip to content
Snippets Groups Projects
Commit 668c125e authored by Artem Dmitriiev's avatar Artem Dmitriiev
Browse files

Issue #3476129 by k.wiktorowicz, a.dmitriiev: Layout Builder – Error when...

parent b0c80dd3
Branches
Tags
1 merge request!82Issue #3476129 by k.wiktorowicz, a.dmitriiev: Layout Builder – Error when...
Pipeline #307437 passed with warnings
......@@ -794,6 +794,96 @@ function frontend_editing_modules_installed($modules, $is_syncing) {
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function frontend_editing_preprocess_block(&$variables) {
// Preprocess layout builder block for entity reference fields.
if ($variables['base_plugin_id'] == 'field_block') {
// Check frontend editing settings.
$config = \Drupal::configFactory()->get('frontend_editing.settings');
// Get supported entity types for frontend editing.
$supported_entity_types = $config->get('entity_types');
// Add a wrapper around entity reference fields, so that they could be updated
// with ajax when one of the elements is updated.
$allowed_field_types = [
'entity_reference_revisions',
'entity_reference',
];
foreach ($variables['content'] as $delta => $entity_reference_item) {
if (!isset($entity_reference_item['#theme'])) {
continue;
}
if ($entity_reference_item['#theme'] != 'field') {
continue;
}
if (in_array($entity_reference_item['#field_type'], $allowed_field_types)) {
$settings = $entity_reference_item['#items']->getFieldDefinition()->getSettings();
// Only add the wrapper for entity reference fields that are targeting the
// supported for frontend editing entity types.
if (!in_array($settings['target_type'], array_keys($supported_entity_types), TRUE)) {
continue;
}
if (!empty($settings['handler_settings']['target_bundles']) && empty(array_intersect($settings['handler_settings']['target_bundles'], $supported_entity_types[$settings['target_type']]))) {
continue;
}
// If ajax content update is not allowed, no need to add field wrapper.
if (!$config->get('ajax_content_update')) {
continue;
}
// Check if the field is not excluded from frontend editing ajax content
// update.
if (!frontend_editing_add_field_wrapper($entity_reference_item['#entity_type'], $entity_reference_item['#bundle'], $entity_reference_item['#field_name'])) {
continue;
}
// Selector should contain the entity type, the entity id and the field
// name that it belongs too.
$parent_view_mode = $entity_reference_item['#view_mode'] == '_custom' ? 'default' : $entity_reference_item['#view_mode'];
$selector = $entity_reference_item['#entity_type'] . '--' . $entity_reference_item['#object']->id() . '--' . $entity_reference_item['#field_name'] . '--' . $parent_view_mode;
// Because it is possible to remove wrappers in the field template and
// there is no reliable way to know whether the field wrapper attributes
// are printed, force add theme wrappers.
$variables['content']['#theme_wrappers'] = [
'container' => [
'#attributes' => [
'data-frontend-editing' => $selector,
'class' => [
'frontend-editing-field-wrapper',
],
],
],
];
// Add parent field view mode to render array of children.
foreach (Element::getVisibleChildren($entity_reference_item) as $field_delta) {
$item = $variables['content'][$delta][$field_delta];
if (!empty($item['#pre_render'])) {
$variables['content'][$delta][$field_delta]['#parent_field_view_mode'] = $variables['content'][$delta]['#view_mode'];
}
}
$variables['content']['frontend_editing'][$entity_reference_item['#field_name'] . '_update_content'] = [
'#type' => 'link',
'#title' => t('Reload content for this field'),
'#url' => Url::fromRoute('frontend_editing.update_content', [
'entity_id' => $entity_reference_item['#object']->id(),
'entity_type_id' => $entity_reference_item['#entity_type'],
'field_name' => $entity_reference_item['#field_name'],
'view_mode' => $parent_view_mode,
]),
'#attributes' => [
'title' => t('Reload content for this field'),
'class' => [
'use-ajax',
'frontend-editing-update-content',
'visually-hidden',
],
'data-fe-update-content' => $selector,
],
];
}
}
}
}
/**
* Implements hook_preprocess_page().
*/
......
......@@ -445,7 +445,43 @@ class FrontendEditingController extends ControllerBase {
if ($message) {
$response->addCommand(new MessageCommand($message, NULL, ['type' => 'error']));
}
$updated_content = $paragraph->getParentEntity()->get($paragraph->get('parent_field_name')->value)->view($view_mode_id);
if ($view_mode_id == '_custom') {
$view_mode_id = 'default';
}
$field_formatter_settings = $view_mode_id;
$parent_entity = $paragraph->getParentEntity();
$parent_field_name = $paragraph->get('parent_field_name')->value;
// Load the view display. We need to know whether it uses layout builder.
$view_display_id = implode('.', [$parent_entity->getEntityTypeId(), $parent_entity->bundle(), $view_mode_id]);
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
$view_display = $this->entityTypeManager()->getStorage('entity_view_display')->load($view_display_id);
if ($view_display) {
$is_layout_builder = (bool) $view_display->getThirdPartySetting('layout_builder', 'enabled');
if ($is_layout_builder) {
$layout_builder_field_block_id_parts = [
'field_block',
$parent_entity->getEntityTypeId(),
$parent_entity->bundle(),
$parent_field_name,
];
$layout_builder_field_block_id = implode(':', $layout_builder_field_block_id_parts);
$sections = $view_display->getThirdPartySetting('layout_builder', 'sections');
/** @var \Drupal\layout_builder\Section $section */
foreach ($sections as $section) {
foreach ($section->getComponents() as $component) {
if ($component->getPluginId() == $layout_builder_field_block_id) {
$configuration = $component->get('configuration');
if (!empty($configuration['formatter'])) {
$field_formatter_settings = $configuration['formatter'];
break 2;
}
}
}
}
}
}
$updated_content = $parent_entity->get($parent_field_name)
->view($field_formatter_settings);
foreach (Element::getVisibleChildren($updated_content) as $delta) {
$item = $updated_content[$delta];
if (!empty($item['#pre_render'])) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment