You are here

function farm_flag_applies in farmOS 2.x

Check to see if a flag applies to an entity type + bundle.

Parameters

\Drupal\farm_flag\Entity\FarmFlagInterface $flag: The flag object.

string|null $entity_type: The entity type machine name.

string|null $bundle: The bundle name.

Return value

bool Returns TRUE if the flag applies, FALSE otherwise.

2 calls to farm_flag_applies()
farm_flag_field_allowed_values in modules/core/flag/farm_flag.module
Allowed values callback function for the flags field.
farm_ui_views_form_views_exposed_form_alter in modules/core/ui/views/farm_ui_views.module
Implements hook_form_BASE_FORM_ID_alter().

File

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

Code

function farm_flag_applies(FarmFlagInterface $flag, $entity_type = NULL, $bundle = NULL) {

  // If no entity type is specified, we assume the flag applies. This ensures
  // it shows in lists/filters where the entity type may not be known.
  if (empty($entity_type)) {
    return TRUE;
  }

  // Load applicable entity types.
  $entity_types = $flag
    ->getEntityTypes();

  // The flag applies if there are no allowed entity types specified.
  if (empty($entity_types)) {
    return TRUE;
  }

  // The flag applies if the entity type is in the list of applicable entity
  // types, and the bundle is in the list of applicable bundles (or the flag
  // applies to "all" bundles).
  if (array_key_exists($entity_type, $entity_types) && (in_array($bundle, $entity_types[$entity_type]) || in_array('all', $entity_types[$entity_type]))) {
    return TRUE;
  }

  // Otherwise, assume the flag does not apply.
  return FALSE;
}