You are here

function farm_flag_field_allowed_values in farmOS 2.x

Allowed values callback function for the flags field.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $definition: The field definition.

\Drupal\Core\Entity\ContentEntityInterface|null $entity: The entity being created if applicable.

Return value

array Returns an array of allowed values for use in form select options.

1 call to farm_flag_field_allowed_values()
EntityFlagActionForm::buildForm in modules/core/flag/src/Form/EntityFlagActionForm.php
Form constructor.
1 string reference to 'farm_flag_field_allowed_values'
farm_flag_entity_base_field_info in modules/core/flag/farm_flag.module
Implements hook_entity_base_field_info().

File

modules/core/flag/farm_flag.module, line 51
The farmOS Flags module.

Code

function farm_flag_field_allowed_values(FieldDefinitionInterface $definition, ContentEntityInterface $entity = NULL) {

  /** @var \Drupal\farm_flag\Entity\FarmFlagInterface[] $flags */
  $flags = \Drupal::entityTypeManager()
    ->getStorage('flag')
    ->loadMultiple();
  $allowed_values = [];
  $entity_type = NULL;
  $bundle = NULL;
  if (!empty($entity)) {
    $entity_type = $entity
      ->getEntityTypeId();
    $bundle = $entity
      ->bundle();
  }
  foreach ($flags as $id => $flag) {
    if (farm_flag_applies($flag, $entity_type, $bundle)) {
      $allowed_values[$id] = $flag
        ->getLabel();
    }
  }
  return $allowed_values;
}