API page: https://api.drupal.org/api/drupal/core%21modules%21options%21options.api...

While trying to use the sample provided on the api page i get the following error:
Error: Call to a member function id() on null in mymodule_options_list_alter()

The field information is stored in $context['fieldDefinition'] object for now.

To get the field name the sample should look the following way.

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

The ->id() function from the existing sample would return bundle / entity_type and the actual fieldname.

Comments

SteffenR created an issue. See original summary.

steffenr’s picture

Status: Active » Needs review
david.gil’s picture

Version: 8.0.x-dev » 8.3.x-dev
Assigned: Unassigned » david.gil
Status: Needs review » Active
Issue tags: +Documentation

I can confirm the problem, i am working in a patch

david.gil’s picture

Hi,

i attach the patch that solves the documentation problem.

Best

david.gil’s picture

Assigned: david.gil » Unassigned
Status: Active » Needs review
keopx’s picture

Component: documentation » options.module
Status: Needs review » Reviewed & tested by the community

Looks fine

  • catch committed 8ee82f2 on 8.3.x
    Issue #2788521 by david.gil, SteffenR: hook_options_list_alter sample...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.3.x and cherry-picked to 8.2.x. Thanks!

  • catch committed d8ecf18 on 8.2.x
    Issue #2788521 by david.gil, SteffenR: hook_options_list_alter sample...

Status: Fixed » Closed (fixed)

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