The Problem:
At present Transitions have labels, these labels do not have to be unique. This means that if you have multiple transitions within a workflow that have the same label it is no longer possible to uniquely identify the permission in the permission listing.
Without desc

Proposed solution:
Add a description that includes the "from" and "to" states to help uniquely identify the correct permission.
Without desc

Comments

alexfarr created an issue. See original summary.

timmillwood’s picture

Status: Active » Needs review
Issue tags: +Needs tests
+++ b/core/modules/content_moderation/src/Permissions.php
@@ -37,4 +41,21 @@ public function transitionPermissions() {
+  private static function getFromDescription($transition) {

Does this need to be a static function?

Status: Needs review » Needs work

The last submitted patch, content_moderation-permissions-description.patch, failed testing. View results

timmillwood’s picture

Status: Needs work » Needs review

Awesome, looks like we already have a test for this, it just needs updating.

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.

alexfarr’s picture

StatusFileSize
new2.34 KB

Thanks for the review Tim, I have actioned the change from a static function to a normal method. I have also updated the test to check for the description.

sam152’s picture

Status: Needs review » Needs work
+++ b/core/modules/content_moderation/src/Permissions.php
@@ -37,4 +41,21 @@ public function transitionPermissions() {
+  /**
+   * @param \Drupal\workflows\TransitionInterface $transition
+   *   Transition to get From list from.
+   *
+   * @return string
+   *   Names of the From transition states.
+   */
+  private function getFromDescription($transition) {
+    $from = [];
+
+    foreach ($transition->from() as $state) {
+      $from[] = $state->label();
+    }
+
+    return implode($from, ', ');
+  }

This method is missing a docblock describing what it does and should also be 'protected' visibility.

We can actually eliminate the method entirely and turn it into a one-liner:

'description' => $this->t('Move content from %from state to %to state.', [
  '%from' => implode(', ', array_map([State::class, 'labelCallback'], $transition->from())),
  '%to' => $transition->to()->label(),
]),

Where 'State' is imported from Drupal\workflows\State.

We could get probably try to get fancy with conditional plural formatting for state/states, but I don't think it's warranted. This is a big improvement as it stands.

alexfarr’s picture

StatusFileSize
new2.12 KB

Simplification made, thanks for the review.

alexfarr’s picture

Status: Needs work » Needs review
sam152’s picture

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

Looks good to me.

alexpott’s picture

Category: Feature request » Task
Status: Reviewed & tested by the community » Needs work

Yep this is better but I think we should use ->formatPlural() to cope with the multiple from states. It's not that much of an onus. And it'll mean that we have the correct strings for translators first time.

Also this is more a task than a feature.

alexfarr’s picture

StatusFileSize
new2.26 KB

Here is the patch with the pluralised message.

alexpott’s picture

Status: Needs work » Needs review
+++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationPermissionsTest.php
@@ -59,9 +59,11 @@ public function permissionsTestCases() {
+            'description' => 'Move content from <em class="placeholder">Draft, Published</em> state to <em class="placeholder">Published</em> state.',
...
+            'description' => 'Move content from <em class="placeholder">Draft, Published</em> state to <em class="placeholder">Draft</em> state.',

Looks like this tests should fail.

Also funnily enough - is the word "state" or "states" that useful? Maybe the message could be "Change state from Draft, Published to Published" hmmm.... but I guess there you'll need an or which is even harder.

Status: Needs review » Needs work

The last submitted patch, 12: content_moderation-permissions-description-2977495-12.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

alexfarr’s picture

StatusFileSize
new2.26 KB

I have updated the test so it does not fail. However, I have not updated the test to include a singular state. I have spent some time trying to get the permissionsTestCases() data provider to build a simple workflow but failed. The alternative would be to add extra fixture data, but I am unsure if this is the right process. I will need some guidance to take these tests further.

alexfarr’s picture

Status: Needs work » Needs review
sam152’s picture

Here is the expanded test case. I had a crack at implementing the suggestion in #13, it looked something like:

diff --git a/core/modules/content_moderation/src/Permissions.php b/core/modules/content_moderation/src/Permissions.php
index c02308c17b..dacbf54f3b 100644
--- a/core/modules/content_moderation/src/Permissions.php
+++ b/core/modules/content_moderation/src/Permissions.php
@@ -26,19 +26,20 @@ public function transitionPermissions() {
     /** @var \Drupal\workflows\WorkflowInterface $workflow */
     foreach (Workflow::loadMultipleByType('content_moderation') as $id => $workflow) {
       foreach ($workflow->getTypePlugin()->getTransitions() as $transition) {
+
+        $from_states = $transition->from();
+        $last_from_state = array_pop($from_states);
+        $from_label = count($from_states) ? sprintf('%s %s %s', implode(', ', array_map([State::class,'labelCallback'], $from_states)), $this->t('or'), $last_from_state->label()) : $last_from_state->label();
+
         $permissions['use ' . $workflow->id() . ' transition ' . $transition->id()] = [
           'title' => $this->t('%workflow workflow: Use %transition transition.', [
             '%workflow' => $workflow->label(),
             '%transition' => $transition->label(),
           ]),
-          'description' => $this->formatPlural(
-            count($transition->from()),
-            'Move content from %from state to %to state.',
-            'Move content from %from states to %to state.', [
-              '%from' => implode(', ', array_map([State::class,'labelCallback'], $transition->from())),
-              '%to' => $transition->to()->label(),
-            ]
-          ),
+          'description' => $this->t('Move content from %from to %to.', [
+            '%from' => $from_label,
+            '%to' => $transition->to()->label(),
+          ]),
         ];
       }
     }

and the test cases ended up looking like:

Array (
    'use simple_workflow transition publish' => Array (
        'title' => '<em class="placeholder">Simpl...ition.'
        'description' => 'Move content from <em class="placeholder">Draft or Published</em> to <em class="placeholder">Published</em>.'
    )
    'use simple_workflow transition create_new_draft' => Array (
        'title' => '<em class="placeholder">Simpl...ition.'
        'description' => 'Move content from <em class="placeholder">Draft or Published</em> to <em class="placeholder">Draft</em>.'
    )
    'use simple_workflow transition archive' => Array (
        'title' => '<em class="placeholder">Simpl...ition.'
        'description' => 'Move content from <em class="placeholder">Published</em> to <em class="placeholder">Archived</em>.'
    )
)

I don't really mind either one, I'm not sure the benefit is worth the complexity, but happy to see what others think. One thing to note is, the "or" would always have to be part of the <em> placeholder string, because there is a dynamic number of from states. Doesn't read that nicely to have parts of the sentence classed with "placeholder", given we're trained to treat that as the "variable" part of messages.

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.

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.

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.

joachim’s picture

Status: Needs review » Reviewed & tested by the community

LGTM.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs re-roll

Needs some help to get past DrupalCI after nearly three years.

neslee canil pinto’s picture

Status: Needs work » Needs review
Issue tags: -Needs re-roll
StatusFileSize
new4.24 KB
new706 bytes

Updated patch as per #24

joachim’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the reroll!

  • catch committed 2cab2f4 on 9.3.x
    Issue #2977495 by alexfarr, Sam152, Neslee Canil Pinto, timmillwood,...

  • catch committed 46444c6 on 9.2.x
    Issue #2977495 by alexfarr, Sam152, Neslee Canil Pinto, timmillwood,...
catch’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.3.x and cherry-picked to 9.2.x, thanks!

Status: Fixed » Closed (fixed)

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