This is a side issue, in response

https://www.drupal.org/node/2584647#comment-11577277

#2

Also, per documentation, action must be one of those values, so why check it, should be checked earlier and an exception thrown or something?

$action must be 'flag' or 'unflag'

Some of the specialist knowledge floating around in the back of my head is that the code has been carefully constructed - so that if the $action get corrupted with a value like "socks" say then the action 'flag' is assumed to be the default value 'flag'.

and by specialist knowledge --- I mean a code smell.

this is encoded in

Drupal\flag\ActionLink\ActionLinkTypeBase::buildLink()

    if ($action === 'unflag') {
      $render['#title'] = $flag->getUnflagShortText();
      $render['#attributes']['title'] = $flag->getUnflagLongText();
      $render['#action'] = 'unflag';
    }
    else {
      $render['#title'] = $flag->getFlagShortText();
      $render['#attributes']['title'] = $flag->getFlagLongText();
      $render['#action'] = 'flag';
    }

this is the subtlety in 'unflag' coming before flag.

I have had to learn this to hard way - I have seen more that a few comments in separate issue suggesting this is wonky/confusing.

I am happy for production code to default to flag - there is no need to throw an exception -
to my mind this is the case for an assert - something that will never hit production.

If other want to update specific documentation or add addition asserts then -- that will come up in review :)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

martin107 created an issue. See original summary.

martin107’s picture

Status: Active » Needs review
FileSize
604 bytes

patch

Status: Needs review » Needs work

The last submitted patch, 2: assert-2794767-2.patch, failed testing.

The last submitted patch, 2: assert-2794767-2.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review

socketwench’s picture

Status: Needs review » Fixed

Thanks, Martin!

andypost’s picture

Status: Fixed » Needs review
+++ b/src/ActionLink/ActionLinkTypeBase.php
@@ -101,6 +101,7 @@ abstract class ActionLinkTypeBase extends PluginBase implements ActionLinkTypePl
+      assert($action === 'flag', '$action must be one of "flag" or "unflag"');

hm, this checks only for "flag" as I see

martin107’s picture

In the full context,-

When the code flows into the bottom half of the if-then structure - the case equality check for "unflag" has already been considered and we are at the point where the developer needs to know there is a problem.

    if ($action === 'unflag') {
      $render['#title'] = $flag->getUnflagShortText();
      $render['#attributes']['title'] = $flag->getUnflagLongText();
      $render['#action'] = 'unflag';
    }
    else {
      assert($action === 'flag', '$action must be one of "flag" or "unflag"');
      $render['#title'] = $flag->getFlagShortText();
      $render['#attributes']['title'] = $flag->getFlagLongText();
      $render['#action'] = 'flag';
    }

I hope this helps

amit.drupal’s picture

@martin107 your comment #9 is helpfull.

andypost’s picture

Status: Needs review » Fixed

Sorry for derail, #9 makes sense

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.