Problem/Motivation

It's takes a lot of time for me to understand this part of the code.
Can we decrease complexity for better code readability?

  public static function validateAllowedValues($element, FormStateInterface $form_state) {
    $items = array_filter(array_map(function ($item) use ($element) {
      $current_element = $element['table'][$item];
      if ($current_element['item']['key']['#value'] !== NULL && $current_element['item']['label']['#value']) {
        return $current_element['item']['key']['#value'] . '|' . $current_element['item']['label']['#value'];
      }
      elseif ($current_element['item']['key']['#value']) {
        return $current_element['item']['key']['#value'];
      }
      elseif ($current_element['item']['label']['#value']) {
        return $current_element['item']['label']['#value'];
      }

      return NULL;
    }, Element::children($element['table'])), function ($item) {
      return $item;
    });

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

  • Discus code complexity.

API changes

Data model changes

Release notes snippet

Issue fork drupal-3403558

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

zniki.ru created an issue. See original summary.

Keshav Patel made their first commit to this issue’s fork.

keshav patel’s picture

Status: Active » Needs review

Changes made in the above MR (#3) to make code more readable, Please review.

nikolay shapovalov’s picture

Status: Needs review » Needs work

Thanks for MR. Can we separate array_filter() and array_map()? For me it's still not obvious what happens in array_map, and what happens in array_filter.

Looks like there is a bug with Drupal.WhiteSpace.ScopeIndent.IncorrectExact phpcs rule inside array_map().

sourabhjain made their first commit to this issue’s fork.

keshav patel’s picture

Status: Needs work » Needs review

Update the MR, Please Review.

array_filter function:
The array_filter function with the provided callback function

function ($item) {
    return $item;
}

removes the empty strings('') and NULL values from $items array, giving out the filtered array.

nikolay shapovalov’s picture

Status: Needs review » Needs work

Tests fail.

keshav patel’s picture

Status: Needs work » Needs review

Nikolay Shapovalov, Re-ran the failed test job "#542553: PHP 8.2 MySQL 8" it Passed!

smustgrave’s picture

Status: Needs review » Needs work

Any new features should have test coverage, but maybe this is a task. Didn't wnat to make that call though.

Also even simple tickets should still have a filled out issue summary.

Thanks.

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.

smustgrave’s picture

Category: Feature request » Task
Status: Needs work » Postponed (maintainer needs more info)

Seems some work was already done wonder if this issue is out of date now?

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Since the summary is incomplete not sure what next step would be. But this function appears to have been updated previously. If still a valid task please please re-open

Did assign credit though.

Thanks.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.