You are here

farm_id_tag.module in farmOS 2.x

ID tag module.

File

modules/core/id_tag/farm_id_tag.module
View source
<?php

/**
 * @file
 * ID tag module.
 */
use Drupal\Core\Entity\EntityTypeInterface;

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

  // Add ID tag field to assets.
  if ($entity_type
    ->id() == 'asset') {
    $field_info = [
      'type' => 'id_tag',
      'label' => t('ID tags'),
      'description' => t('List any identification tags that this asset has. Use the fields below to describe the type, location, and ID of each.'),
      'multiple' => TRUE,
      'weight' => [
        'form' => 50,
        'view' => 50,
      ],
    ];
    $fields['id_tag'] = \Drupal::service('farm_field.factory')
      ->baseFieldDefinition($field_info);
  }
  return $fields;
}

/**
 * Allowed values callback function for the ID tag type field.
 *
 * @param string $bundle
 *   The asset bundle to get allowed values for.
 *
 * @return array
 *   Returns an array of allowed values for use in form select options.
 */
function farm_id_tag_type_allowed_values($bundle) {

  /** @var \Drupal\farm_id_tag\Entity\FarmIDTagTypeInterface[] $types */
  $types = \Drupal::entityTypeManager()
    ->getStorage('tag_type')
    ->loadMultiple();
  $allowed_values = [];
  foreach ($types as $id => $type) {
    $bundles = $type
      ->getBundles();
    if (empty($bundles) || in_array($bundle, $bundles)) {
      $allowed_values[$id] = $type
        ->getLabel();
    }
  }
  return $allowed_values;
}

/**
 * Implements hook_theme().
 */
function farm_id_tag_theme() {
  return [
    'field__id_tag' => [
      'template' => 'field--id-tag',
      'base hook' => 'field',
    ],
  ];
}

Functions

Namesort descending Description
farm_id_tag_entity_base_field_info Implements hook_entity_base_field_info().
farm_id_tag_theme Implements hook_theme().
farm_id_tag_type_allowed_values Allowed values callback function for the ID tag type field.