Problem/Motivation

There is a code snippet from options.api.php file:

function hook_options_list_alter(array &$options, array $context) {
  // Check if this is the field we want to change.
  if ($context['fieldDefinition']->id() == 'field_option') {
    // Change the label of the empty option.
    $options['_none'] = t('== Empty ==');
  }
}

This piece of code assumes that $context['fieldDefinition']->id() returns a field name, but it's not correct. See:
debug console

As we can see, id() method returns a full identifier, that contains entity type, bundle, and field name. getName() returns the expected value.

Proposed resolution

Replace $context['fieldDefinition']->id() with $context['fieldDefinition']->getName().

Issue fork drupal-3190231

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

Matroskeen created an issue. See original summary.

matroskeen’s picture

Issue summary: View changes
Status: Active » Needs review
larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Straight forward fix, thanks

  • catch committed 8d2875c on 9.2.x
    Issue #3190231 by Matroskeen: Incorrect example of checking field name...

  • catch committed 7636de0 on 9.1.x
    Issue #3190231 by Matroskeen: Incorrect example of checking field name...
catch’s picture

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

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

Status: Fixed » Closed (fixed)

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