Problem/Motivation

Currently when creating link to open in modal the developer must do something like:

'normal_modal' => [
        '#title' => 'Normal Modal!',
        '#type' => 'link',
        '#url' => Url::fromRoute('dialog_renderer_test.modal_content'),
        '#attributes' => [
          'class' => ['use-ajax'],
          'data-dialog-type' => 'modal',
        ],
        '#attached' => [
          'library' => [
            'core/drupal.ajax',
          ],
        ],
      ],

To use the off-canvas dialog:

'off_canvas_link_1' => [
        '#title' => 'Click Me 1!',
        '#type' => 'link',
        '#url' => Url::fromRoute('off_canvas_test.thing1'),
        '#attributes' => [
          'class' => ['use-ajax'],
          'data-dialog-type' => 'dialog',
          'data-dialog-renderer' => 'off_canvas',
        ],
      ],

In both cases the developer must add the class 'use-ajax' but all dialogs like this will use the ajax system.

This is confusing because it gives the impression that you could make a dialog like and have it not use ajax if you left off this class but in fact if you leave off this class the link will not be a dialog link at all.

Proposed resolution

Automatically add the 'use-ajax' class if data-dialog-type is provided.

Remaining tasks

Review

User interface changes

None

API changes

To make a dialog link you won't have to add 'use-ajax' class manually.

Data model changes

None

Comments

tedbow created an issue. See original summary.

tedbow’s picture

Status: Active » Needs review
StatusFileSize
new2.86 KB

Here is the patch.

Not sure where else we should add test coverage

Status: Needs review » Needs work

The last submitted patch, 2: 2933379-2.patch, failed testing. View results

tedbow’s picture

Status: Needs work » Needs review

Tested passed after DrupalCI error

berdir’s picture

+++ b/core/modules/system/system.module
@@ -1534,3 +1534,15 @@ function system_element_info_alter(&$type) {
+ * Implements hook_link_alter().
+ */
+function system_link_alter(&$variables) {
+  // If a dialog type is provided add 'use-ajax' class.
+  if (!empty($variables['options']['attributes']['data-dialog-type'])) {
+    if (!isset($variables['options']['attributes']['class']) || array_search('use-ajax', $variables['options']['attributes']['class']) === FALSE) {
+      $variables['options']['attributes']['class'][] = 'use-ajax';
+    }
+  }

Why not deal with this directly in \Drupal\Core\Render\Element\Link::preRenderLink()? We already have ajax stuff there.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

borisson_’s picture

Status: Needs review » Needs work

I think that #5 makes sense, let's move it there, we can keep the testcoverage added in #2.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

matroskeen’s picture

Status: Needs work » Needs review
StatusFileSize
new3.08 KB

I was using dialog windows recently, so I think it can be a nice improvement of developer experience.
Here is a re-roll that contains 2 minor changes:
1) Moved code from system_link_alter() to Drupal\Core\Render\Element\Link::preRenderLink();
2) Replaced assertNotContains() with assertStringNotContainsString();

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

tanubansal’s picture

Changes mentioned in #12 are successfully applied on 9.1

tedbow’s picture

StatusFileSize
new1.86 KB
new3.26 KB

@Matroskeen thanks for the fixes. I think this changes are good. I would RTBC but since I create the original patch I don't think I should.

but looking at the patch I think there are a couple comments and string changes we could add to make it more clear what we are testing.
Hopefully this is clearer

  1. +++ b/core/modules/system/tests/modules/dialog_renderer_test/src/Controller/TestController.php
    @@ -44,6 +44,19 @@ public function linksDisplay() {
    +      'normal_modal_class_added' => [
    +        '#title' => 'Normal Modal Class Added!',
    

    Looking a back at this the array key and the link title are confusing. "Normal Module Class Added" to me now reads as there is class "normal-modal" and it was added. Changing both of this to be more clear.

  2. +++ b/core/modules/system/tests/src/FunctionalJavascript/ModalRendererTest.php
    @@ -33,6 +33,12 @@ public function testModalRenderer() {
    +    $this->clickLink('Normal Modal Class Added!');
    +    // Neither of the wide modals should have been used.
    

    We don't have a comment here as to what we are actually testing and the link title is confusing. Adding a comment.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ranjith_kumar_k_u’s picture

.

ranjith_kumar_k_u’s picture

StatusFileSize
new3.29 KB

Re-rolled #15 for 9.4

ranjith_kumar_k_u’s picture

StatusFileSize
new3.33 KB
new915 bytes

Status: Needs review » Needs work

The last submitted patch, 20: 2933379-20.patch, failed testing. View results

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smethawee’s picture

Is 9.4.8 working? and how to fix this issue. Could anybody help please.

Thanks!

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.