Problem/Motivation

See referenced issues. Module-exist checks have been put in place in multiple core components that reference other core components in order to prevent a PHP exception when the referenced component is not enabled/installed. In some cases, the inline module exists checks make the code less readable.

Proposed resolution

The results of some of those inline checks can be moved into local variables in order to make the code more readable.

See contact_help() for an example of how to deal with this.

$block_page = \Drupal::moduleHandler()->moduleExists('block') ? \Drupal::url('block.admin_display') : '#';

Above line is an example of moving the inline check to a local variable.

Remaining tasks

Identify and replace inline module-exists checks with local variable equivalents where it will improve readability.

User interface changes

None

API changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

opratr’s picture

Working on new patch now.

opratr’s picture

Issue summary: View changes
opratr’s picture

Priority: Major » Normal
opratr’s picture

Status: Active » Needs review
FileSize
74.67 KB

Status: Needs review » Needs work

The last submitted patch, 4: refactor_certain_inline-2492505-4.patch, failed testing.

opratr’s picture

Status: Needs work » Needs review
FileSize
74.85 KB
jhodgdon’s picture

Status: Needs review » Needs work
Issue tags: +Needs manual testing

This is mostly great! I think we should make sure to do a manual test of all the changed help and other pages before we mark this RTBC and commit it, to make sure all the links are working.

A few comments, and one thing to fix:

  1. +++ b/core/modules/aggregator/aggregator.module
    @@ -20,6 +20,7 @@
    +      $block_page = \Drupal::moduleHandler()->moduleExists('block') ? \Drupal::url('block.admin_display') : '#';
    

    Since $block_page is already needed in two places in this same function, how about defining the variable outside the case statement, rather than doing it twice, one in each case?

    Well, I guess that would be marginally less efficient for the cases that don't need it. So, either way. For better maintainability and because I think the efficiency hit is very small (and I hate copy/paste of code in general), I would be in favor of moving it outside the switch, but we could go either way.

  2. +++ b/core/modules/field/field.module
    @@ -64,10 +64,10 @@
    -      $field_ui_url = \Drupal::moduleHandler()->moduleExists('field_ui') ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#';
    +      $field_ui_page = \Drupal::moduleHandler()->moduleExists('field_ui') ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#';
           $output = '';
    

    This looks suspicious, but I verified that $field_ui_url is not being used anywhere, so it's fine.

  3. +++ b/core/modules/help/help.api.php
    @@ -46,7 +46,8 @@ function hook_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_
         case 'help.page.block':
    -      return '<p>' . t('Blocks are boxes of content rendered into an area, or region, of a web page. The default theme Bartik, for example, implements the regions "Sidebar first", "Sidebar second", "Featured", "Content", "Header", "Footer", etc., and a block may appear in any one of these areas. The <a href="!blocks">blocks administration page</a> provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions.', array('!blocks' => \Drupal::url('block.admin_display'))) . '</p>';
    +      $block_page = \Drupal::moduleHandler()->moduleExists('block') ? \Drupal::url('block.admin_display') : '#';
    +      return '<p>' . t('Blocks are boxes of content rendered into an area, or region, of a web page. The default theme Bartik, for example, implements the regions "Sidebar first", "Sidebar second", "Featured", "Content", "Header", "Footer", etc., and a block may appear in any one of these areas. The <a href="!blocks">blocks administration page</a> provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions.', array('!blocks' => $block_page)) . '</p>';
     
    

    In this file, we should not be making this change.

    This is the sample function body for the hook_help() hook, and the example it's using is for the Block module, so we do not need to check whether Block is enabled here.

opratr’s picture

Thanks for the review jhodgdon!

Response to your comments:

  1. I had actually originally put the $block_page variable outside of the case statements for exactly that same reasoning, but apparently a new Test that was introduced on June 1 was failing because of it. I didn't check the test code in detail but the comments around it indicated that new checks were either put in place or will be put in place to make sure nobody places code between the hook_help() function declaration and the select block to prevent URLGenerator performance issues. So, I refactored and moved the variable declaration into each case statement and the tests started passing. :)
  2. Yes, it looked like someone partially started to fix that issue by declaring the $field_ui_url variable, but never used it. I finished the job, and renamed the variable to match the naming convention I've been using in all other related fixes.
  3. Oops, missed that detail. I'll get this one fixed ASAP
umarzaffer’s picture

Status: Needs work » Needs review
FileSize
1.44 KB
73.27 KB

Have made changes as per #7.3 and manually tested the help pages. Everything looks good to me.

opratr’s picture

@umarzaffer Thanks for the patch!

Next time, please leave a note on the issue if you decide to work on a patch that is being actively worked on, especially if the issue is assigned to someone. In this case it was only a 5 minute fix, but on other issues it could mean hours of wasted work for someone that doesn't know that you're performing duplicate work.

umarzaffer’s picture

Apologies @opratr. I realized that a bit late and in fact sent you an apology message as well. Never meant to work on an issue assigned to someone. Was a miss from my side. All in good faith!

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs manual testing

Great, meant in good faith I'm sure. We all have to learn the procedures at some point. :) Anyway, the manual testing is much appreciated @umarzaffer, and we needed a new patch one way or another. Thanks for the gentle handling of the problem @opratr.

One way or another, the new patch looks good. Thanks!

opratr’s picture

Assigned: opratr » Unassigned
opratr’s picture

No worries @umarzaffer! Got your message. No harm done. Thank you. :)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: refactor_certain_inline-2492505-9.patch, failed testing.

Status: Needs work » Needs review
jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Back to previous status after green retest.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs beta evaluation, +Usability

Thanks everyone for your work here!

  1. My first reaction to this issue is that it needs a beta evaluation and the case for making the change during the beta is not clear. See the allowed beta changes policy. The patch is not disruptive for contrib or existing sites, but it is the type of patch that needs frequent rerolls, takes disproportionate time to review, and may introduce the need for non-trivial rerolls in other patches. Furthermore, even if there were none of this disruption, any normal task with no prioritized changes should be postponed unless there is a special circumstance.
     

  2. Leaving aside my first point and looking at the patch:

    +++ b/core/modules/aggregator/aggregator.module
    @@ -44,9 +45,10 @@ function aggregator_help($route_name, RouteMatchInterface $route_match) {
    +      $block_page = \Drupal::moduleHandler()->moduleExists('block') ? \Drupal::url('block.admin_display') : '#';
    

    We're repeating this line a lot (maybe 26 times or so based on the diffstat of this patch?). That would suggest to me that we should refactor this into a method (or a function in help.module even). That said though...
     

  3. I do not think linking to # is the correct behavior. A link that basically does nothing is a usability and accessibility problem. It is also not semantic for a resource that does not exist. (See my comment in #2498675-5: Linking to a non-existent route should not throw an exception.)
     

I recognize for point 3 is not introduced by this patch. However, it would be reason to mark this issue wontfix, and we could instead improve this as a part of fixing that bug.

xjm’s picture

Oh, I forgot to mention that I discussed this with @alexpott as well.

I think that either the entire sentence should be conditionally displayed depending on whether or not the module is available, or the help text should be written in such a way that it's relevant whether or not the module is enabled (and possibly even informs you if the module is not available). @alexpott preferred the second option.

Another issue though is that even if the module is enabled, the user might not have permission to access the linked resource. I believe in some places we do a permission check and add the link or paragraph conditionally based on that as well (or we have in the past). At least, there have been conditionals like this in hook_help() implementations for sure.

jhodgdon’s picture

We *want* the sentence to be there, actually, because it would tell people "You could enable this other module to get this functionality". I agree linking to # is not ideal.

Probably then ideally what we want to do is make the substitution into the t() function either be <a href="...">Block list page</a> or just Block list page. However I think this would be very difficult for translators if not impossible.

So I think we should leave things as they are.

DuaelFr’s picture

Issue tags: -Novice

Removing the Novice tag as this issue is lacking a consensus.
https://www.drupal.org/core-mentoring/novice-tasks#avoid

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.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.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.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.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.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.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.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.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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.

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.