Skip to content
Snippets Groups Projects
Verified Commit 3cf0c074 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3435906 by godotislate, afoerster: Default Images not rendered in layout builder

parent f75c4ad0
No related branches found
No related tags found
28 merge requests!11131[10.4.x-only-DO-NOT-MERGE]: Issue ##2842525 Ajax attached to Views exposed filter form does not trigger callbacks,!9470[10.3.x-only-DO-NOT-MERGE]: #3331771 Fix file_get_contents(): Passing null to parameter,!8540Issue #3457061: Bootstrap Modal dialog Not closing after 10.3.0 Update,!8528Issue #3456871 by Tim Bozeman: Support NULL services,!8373Issue #3427374 by danflanagan8, Vighneshh: taxonomy_tid ViewsArgumentDefault...,!7526Expose roles in response,!7352Draft: Resolve #3203489 "Set filename as",!3878Removed unused condition head title for views,!3818Issue #2140179: $entity->original gets stale between updates,!3742Issue #3328429: Create item list field formatter for displaying ordered and unordered lists,!3731Claro: role=button on status report items,!3651Issue #3347736: Create new SDC component for Olivero (header-search),!3531Issue #3336994: StringFormatter always displays links to entity even if the user in context does not have access,!3355Issue #3209129: Scrolling problems when adding a block via layout builder,!3154Fixes #2987987 - CSRF token validation broken on routes with optional parameters.,!3133core/modules/system/css/components/hidden.module.css,!2812Issue #3312049: [Followup] Fix Drupal.Commenting.FunctionComment.MissingReturnType returns for NULL,!2794Issue #3100732: Allow specifying `meta` data on JSON:API objects,!2378Issue #2875033: Optimize joins and table selection in SQL entity query implementation,!2062Issue #3246454: Add weekly granularity to views date sort,!1105Issue #3025039: New non translatable field on translatable content throws error,!1073issue #3191727: Focus states on mobile second level navigation items fixed,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!877Issue #2708101: Default value for link text is not saved,!617Issue #3043725: Provide a Entity Handler for user cancelation,!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493
Pipeline #133197 passed
Pipeline: drupal

#133200

    ......@@ -22,6 +22,7 @@
    use Drupal\Core\Plugin\ContextAwarePluginInterface;
    use Drupal\Core\Session\AccountInterface;
    use Drupal\Core\StringTranslation\TranslatableMarkup;
    use Drupal\field\FieldConfigInterface;
    use Drupal\layout_builder\Plugin\Derivative\FieldBlockDeriver;
    use Psr\Log\LoggerInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    ......@@ -212,13 +213,7 @@ protected function blockAccess(AccountInterface $account) {
    }
    // Check to see if the field has any values or a default value.
    if ($field->isEmpty() && !$field->getFieldDefinition()->getDefaultValue($entity)) {
    // @todo Remove special handling of image fields after
    // https://www.drupal.org/project/drupal/issues/3005528.
    if ($field->getFieldDefinition()->getType() === 'image' && !empty($field->getFieldDefinition()->getSetting('default_image')['uuid'])) {
    return $access;
    }
    if ($field->isEmpty() && !$this->entityFieldHasDefaultValue()) {
    return $access->andIf(AccessResult::forbidden());
    }
    return $access;
    ......@@ -414,4 +409,33 @@ protected function getFormatter(array $parents, FormStateInterface $form_state)
    ]);
    }
    /**
    * Checks whether there is a default value set on the field.
    *
    * @return bool
    * TRUE if default value set, FALSE otherwise.
    */
    protected function entityFieldHasDefaultValue(): bool {
    $entity = $this->getEntity();
    $field = $entity->get($this->fieldName);
    $definition = $field->getFieldDefinition();
    if ($definition->getDefaultValue($entity)) {
    return TRUE;
    }
    // @todo Remove special handling of image fields after
    // https://www.drupal.org/project/drupal/issues/3005528.
    if ($definition->getType() !== 'image') {
    return FALSE;
    }
    $default_image = $definition->getSetting('default_image');
    // If we are dealing with a configurable field, look in both instance-level
    // and field-level settings.
    if (empty($default_image['uuid']) && ($definition instanceof FieldConfigInterface)) {
    $default_image = $definition->getFieldStorageDefinition()->getSetting('default_image');
    }
    return !empty($default_image['uuid']);
    }
    }
    ......@@ -66,14 +66,22 @@ protected function setUp(): void {
    'field_string_with_default' => 'It is ok to be different',
    'field_string_with_callback' => 'Not from a callback',
    'field_string_late_default' => 'I am way ahead of you.',
    'field_image_with_default' => [
    'target_id' => 2,
    'alt' => 'My different alt text',
    ],
    'field_image_no_default' => [
    'field_image_storage_default' => [
    'target_id' => 3,
    'alt' => 'My third alt text',
    ],
    'field_image_instance_default' => [
    'target_id' => 4,
    'alt' => 'My fourth alt text',
    ],
    'field_image_both_defaults' => [
    'target_id' => 5,
    'alt' => 'My fifth alt text',
    ],
    'field_image_no_default' => [
    'target_id' => 6,
    'alt' => 'My sixth alt text',
    ],
    ]);
    // Create node 2 relying on defaults.
    ......@@ -127,16 +135,31 @@ protected function assertNodeWithValues() {
    $assert_session->pageTextContains('field_string_late_default');
    $assert_session->pageTextNotContains('Too late!');
    $assert_session->pageTextContains('I am way ahead of you');
    // Image field with default should render non-default value.
    $assert_session->pageTextContains('field_image_with_default');
    $assert_session->responseNotContains('My default alt text');
    // Image field with storage default should render non-default value.
    $assert_session->pageTextContains('field_image_storage_default');
    $assert_session->responseNotContains('My storage default alt text');
    $assert_session->responseNotContains('test-file-1');
    $assert_session->responseContains('My different alt text');
    $assert_session->responseContains('test-file-2');
    // Image field with no default should render a value.
    $assert_session->pageTextContains('field_image_no_default');
    $assert_session->responseContains('My third alt text');
    $assert_session->responseContains('test-file-3');
    // Image field with instance default should render non-default value.
    $assert_session->pageTextContains('field_image_instance_default');
    $assert_session->responseNotContains('My instance default alt text');
    $assert_session->responseNotContains('test-file-1');
    $assert_session->responseContains('My fourth alt text');
    $assert_session->responseContains('test-file-4');
    // Image field with both storage and instance defaults should render
    // non-default value.
    $assert_session->pageTextContains('field_image_both_defaults');
    $assert_session->responseNotContains('My storage default alt text');
    $assert_session->responseNotContains('My instance default alt text');
    $assert_session->responseNotContains('test-file-1');
    $assert_session->responseNotContains('test-file-2');
    $assert_session->responseContains('My fifth alt text');
    $assert_session->responseContains('test-file-5');
    // Image field with no default should render a value.
    $assert_session->pageTextContains('field_image_no_default');
    $assert_session->responseContains('My sixth alt text');
    $assert_session->responseContains('test-file-6');
    }
    /**
    ......@@ -166,9 +189,15 @@ protected function assertNodeWithDefaultValues() {
    $assert_session->pageTextNotContains('field_string_late_default');
    $assert_session->pageTextNotContains('Too late!');
    // Image field with default should render default value.
    $assert_session->pageTextContains('field_image_with_default');
    $assert_session->responseContains('My default alt text');
    $assert_session->pageTextContains('field_image_storage_default');
    $assert_session->responseContains('My storage default alt text');
    $assert_session->responseContains('test-file-1');
    $assert_session->pageTextContains('field_image_instance_default');
    $assert_session->responseContains('My instance default alt text');
    $assert_session->responseContains('test-file-1');
    $assert_session->pageTextContains('field_image_both_defaults');
    $assert_session->responseContains('My instance default alt text');
    $assert_session->responseContains('test-file-2');
    // Image field with no default should not render.
    $assert_session->pageTextNotContains('field_image_no_default');
    // Confirm that there is no DOM element for the field_image_with_no_default
    ......@@ -259,7 +288,7 @@ protected function addImageFields() {
    // Create files to use as the default images.
    $files = $this->drupalGetTestFiles('image');
    $images = [];
    for ($i = 1; $i <= 3; $i++) {
    for ($i = 1; $i <= 6; $i++) {
    $filename = "test-file-$i";
    $desired_filepath = 'public://' . $filename;
    \Drupal::service('file_system')->copy($files[0]->uri, $desired_filepath, FileSystemInterface::EXISTS_ERROR);
    ......@@ -272,17 +301,57 @@ protected function addImageFields() {
    $images[] = $file;
    }
    $field_name = 'field_image_with_default';
    $field_name = 'field_image_storage_default';
    $storage_settings['default_image'] = [
    'uuid' => $images[0]->uuid(),
    'alt' => 'My default alt text',
    'alt' => 'My storage default alt text',
    'title' => '',
    'width' => 0,
    'height' => 0,
    ];
    $field_settings['default_image'] = [
    'uuid' => NULL,
    'alt' => '',
    'title' => '',
    'width' => NULL,
    'height' => NULL,
    ];
    $widget_settings = [
    'preview_image_style' => 'medium',
    ];
    $this->createImageField($field_name, 'test_node_type', $storage_settings, $field_settings, $widget_settings);
    $field_name = 'field_image_instance_default';
    $storage_settings['default_image'] = [
    'uuid' => NULL,
    'alt' => '',
    'title' => '',
    'width' => NULL,
    'height' => NULL,
    ];
    $field_settings['default_image'] = [
    'uuid' => $images[0]->uuid(),
    'alt' => 'My default alt text',
    'alt' => 'My instance default alt text',
    'title' => '',
    'width' => 0,
    'height' => 0,
    ];
    $widget_settings = [
    'preview_image_style' => 'medium',
    ];
    $this->createImageField($field_name, 'test_node_type', $storage_settings, $field_settings, $widget_settings);
    $field_name = 'field_image_both_defaults';
    $storage_settings['default_image'] = [
    'uuid' => $images[0]->uuid(),
    'alt' => 'My storage default alt text',
    'title' => '',
    'width' => 0,
    'height' => 0,
    ];
    $field_settings['default_image'] = [
    'uuid' => $images[1]->uuid(),
    'alt' => 'My instance default alt text',
    'title' => '',
    'width' => 0,
    'height' => 0,
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment