Commit 1d7a5db4 authored by Nacho Salvador's avatar Nacho Salvador Committed by Chris Burge
Browse files

Issue #3267358 by nachosalvador, plopesc: Add the language option in the operation link

parent 797c78ac
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -27,5 +27,8 @@
    "minimum-stability": "dev",
    "require": {
        "drupal/core": "^8.8.4 || ^9"
    },
    "require-dev": {
        "drupal/layout_builder_at": "^2.12"
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -5,3 +5,5 @@ package: Layout Builder
core_version_requirement: ^8.8.4 || ^9
dependencies:
  - drupal:layout_builder
test_dependencies:
  - layout_builder_at:layout_builder_at
+14 −1
Original line number Diff line number Diff line
@@ -6,8 +6,10 @@
 */

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;

/**
 * Implements hook_entity_operation().
@@ -20,6 +22,7 @@ function layout_builder_operation_link_entity_operation(EntityInterface $entity)
  $route_parameters = [
    $entity_type_id => $entity->id(),
  ];
  $route_options = [];

  // If current user has access to route, then add the operation link. The
  // access check will only return TRUE if the bundle is Layout Builder-
@@ -29,11 +32,21 @@ function layout_builder_operation_link_entity_operation(EntityInterface $entity)
    return;
  }

  // Add entity's language as a route option if layouts are translatable
  // (e.g. the Layout Builder Asymmetric Translation contrib module
  // is installed).
  if ($entity instanceof FieldableEntityInterface
    && $entity->hasField(OverridesSectionStorage::FIELD_NAME)
    && $entity->get(OverridesSectionStorage::FIELD_NAME)->getFieldDefinition()->isTranslatable()
  ) {
    $route_options['language'] = $entity->language();
  }

  return [
    'layout' => [
      'title' => t('Layout'),
      'weight' => 50,
      'url' => Url::fromRoute($route_name, $route_parameters),
      'url' => Url::fromRoute($route_name, $route_parameters, $route_options),
    ],
  ];
}
+35 −2
Original line number Diff line number Diff line
@@ -2,8 +2,10 @@

namespace Drupal\Tests\layout_builder_operation_link\Functional;

use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;

/**
 * Tests Layout Builder Operation Link.
@@ -23,9 +25,13 @@ class OperationLinkTest extends BrowserTestBase {
   * {@inheritdoc}
   */
  protected static $modules = [
    'content_translation',
    'language',
    'layout_builder_at',
    'layout_builder_operation_link',
    'node',
    'taxonomy',
    'views',
  ];

  /**
@@ -34,6 +40,11 @@ class OperationLinkTest extends BrowserTestBase {
  public function testOperationLink() {
    $assert_session = $this->assertSession();
    $page = $this->getSession()->getPage();
    $additional_langcode = 'es';

    // Enable additional language.
    ConfigurableLanguage::createFromLangcode($additional_langcode)
      ->save();

    // Create user without Layout Builder permissions.
    $auth_user = $this->drupalCreateUser([
@@ -49,13 +60,16 @@ class OperationLinkTest extends BrowserTestBase {
      'access administration pages',
      'access content overview',
      'access taxonomy overview',
      'administer taxonomy',
      'administer content translation',
      'administer languages',
      'administer node display',
      'administer node fields',
      'administer taxonomy',
      'administer taxonomy_term display',
      'administer taxonomy_term fields',
      'bypass node access',
      'configure any layout',
      'translate any entity',
    ]);
    $this->drupalLogin($layout_user);

@@ -63,6 +77,11 @@ class OperationLinkTest extends BrowserTestBase {
    $this->createContentType(['type' => 'page']);
    $this->createContentType(['type' => 'bundle_with_section_field']);

    // Enable translation for bundle_with_section_field bundle and ensure the
    // change is picked up.
    \Drupal::service('content_translation.manager')
      ->setEnabled('node', 'bundle_with_section_field', TRUE);

    // Enable Layout Builder w/ overrides for bundle_with_section_field bundle.
    $this->drupalGet("admin/structure/types/manage/bundle_with_section_field/display/default");
    $page->checkField('layout[enabled]');
@@ -74,10 +93,23 @@ class OperationLinkTest extends BrowserTestBase {
    $this->createNode([
      'type' => 'page',
    ]);
    $this->createNode([
    $bundle_with_section_field = $this->createNode([
      'type' => 'bundle_with_section_field',
    ]);

    // Create a translation through the UI.
    $url_options = ['language' => \Drupal::languageManager()->getLanguage($additional_langcode)];
    $this->drupalGet('node/' . $bundle_with_section_field->id() . '/translations/add/en/' . $additional_langcode, $url_options);
    $this->getSession()->getPage()->pressButton('Save (this translation)');

    // Make layout builder field translatable.
    $this->drupalGet('admin/config/regional/content-language');
    $edit = [
      'entity_types[node]' => TRUE,
      'settings[node][bundle_with_section_field][fields][' . OverridesSectionStorage::FIELD_NAME . ']' => TRUE,
    ];
    $this->submitForm($edit, 'Save configuration');

    // Create taxonomy vocabularies.
    $vocabulary = $this->createVocabulary();
    $vocabulary_with_section_field = $this->createVocabulary();
@@ -102,6 +134,7 @@ class OperationLinkTest extends BrowserTestBase {

    $assert_session->elementNotExists('xpath', '//table//ul[contains(@class, "dropbutton")]//a[contains(@href, "node/1/layout")]');
    $assert_session->elementExists('xpath', '//table//ul[contains(@class, "dropbutton")]//a[contains(@href, "node/2/layout")]');
    $assert_session->elementExists('xpath', '//table//ul[contains(@class, "dropbutton")]//a[contains(@href, "' . $additional_langcode . '/node/2/layout")]');

    $this->drupalGet("admin/structure/taxonomy/manage/$vocabulary_id/overview/");