You are here

farm_flag.module in farmOS 2.x

The farmOS Flags module.

File

modules/core/flag/farm_flag.module
View source
<?php

/**
 * @file
 * The farmOS Flags module.
 */
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\farm_flag\Entity\FarmFlagInterface;
use Drupal\farm_flag\Form\EntityFlagActionForm;
use Drupal\farm_flag\Routing\EntityFlagActionRouteProvider;

/**
 * Implements hook_entity_base_field_info().
 */
function farm_flag_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];

  // Add flag field to farmOS entities.
  if (in_array($entity_type
    ->id(), [
    'asset',
    'log',
    'plan',
  ])) {
    $field_info = [
      'type' => 'list_string',
      'label' => t('Flags'),
      'description' => t('Add flags to enable better sorting and filtering of records.'),
      'allowed_values_function' => 'farm_flag_field_allowed_values',
      'multiple' => TRUE,
      'weight' => [
        'form' => -75,
        'view' => -75,
      ],
    ];
    $fields['flag'] = \Drupal::service('farm_field.factory')
      ->baseFieldDefinition($field_info);
  }
  return $fields;
}

/**
 * Allowed values callback function for the flags field.
 *
 * @param \Drupal\Core\Field\FieldDefinitionInterface $definition
 *   The field definition.
 * @param \Drupal\Core\Entity\ContentEntityInterface|null $entity
 *   The entity being created if applicable.
 *
 * @return array
 *   Returns an array of allowed values for use in form select options.
 */
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;
}

/**
 * Check to see if a flag applies to an entity type + bundle.
 *
 * @param \Drupal\farm_flag\Entity\FarmFlagInterface $flag
 *   The flag object.
 * @param string|null $entity_type
 *   The entity type machine name.
 * @param string|null $bundle
 *   The bundle name.
 *
 * @return bool
 *   Returns TRUE if the flag applies, FALSE otherwise.
 */
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;
}

/**
 * Implements hook_theme().
 */
function farm_flag_theme() {
  return [
    'field__flag' => [
      'base hook' => 'field',
    ],
  ];
}

/**
 * Prepares variables for field--flag templates.
 *
 * Adds classes to each flag wrapper.
 *
 * Default template: field--flag.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: An associative array containing render arrays for the list of
 *     flags.
 */
function template_preprocess_field__flag(array &$variables) {

  // Preprocess list_string flag fields.
  if ($variables['element']['#field_type'] == 'list_string') {

    /** @var \Drupal\Core\Field\FieldItemListInterface $items */
    $items = $variables['element']['#items'];

    // Add classes to each flag.
    foreach ($items as $key => $list_item) {
      $classes = [
        'flag',
        'flag--' . $list_item
          ->getString(),
      ];
      $variables['items'][$key]['attributes']
        ->addClass($classes);
    }
  }
}

/**
 * Implements hook_entity_type_build().
 */
function farm_flag_entity_type_build(array &$entity_types) {

  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */

  // Enable the entity flag action on entity types with a flag field.
  foreach ([
    'asset',
    'log',
    'plan',
  ] as $entity_type) {
    if (!empty($entity_types[$entity_type])) {
      $route_providers = $entity_types[$entity_type]
        ->getRouteProviderClasses();
      $route_providers['flag'] = EntityFlagActionRouteProvider::class;
      $entity_types[$entity_type]
        ->setHandlerClass('route_provider', $route_providers);
      $entity_types[$entity_type]
        ->setLinkTemplate('flag-action-form', '/' . $entity_type . '/flag');
      $entity_types[$entity_type]
        ->setFormClass('flag-action-form', EntityFlagActionForm::class);
    }
  }
}

Functions

Namesort descending Description
farm_flag_applies Check to see if a flag applies to an entity type + bundle.
farm_flag_entity_base_field_info Implements hook_entity_base_field_info().
farm_flag_entity_type_build Implements hook_entity_type_build().
farm_flag_field_allowed_values Allowed values callback function for the flags field.
farm_flag_theme Implements hook_theme().
template_preprocess_field__flag Prepares variables for field--flag templates.