You are here

farm_location.module in farmOS 2.x

File

modules/core/location/farm_location.module
View source
<?php

/**
 * @file
 * Contains farm_location.module.
 */
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function farm_location_form_asset_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Check if the form has the required location fields.
  if (isset($form['intrinsic_geometry']) && isset($form['is_fixed'])) {

    // Set the visible state of the asset.intrinsic_geometry field.
    // Only display if is_fixed is checked.
    $form['intrinsic_geometry']['#states']['visible'] = [
      ':input[name="is_fixed[value]"]' => [
        'checked' => TRUE,
      ],
    ];
  }
}

/**
 * Implements hook_entity_base_field_info().
 */
function farm_location_entity_base_field_info(EntityTypeInterface $entity_type) {
  module_load_include('inc', 'farm_location', 'farm_location.base_fields');
  switch ($entity_type
    ->id()) {

    // Build asset base fields.
    case 'asset':
      return farm_location_asset_base_fields();

    // Build log base fields.
    case 'log':
      return farm_location_log_base_fields();
    default:
      return [];
  }
}

/**
 * Sets the default value for asset is_location boolean field.
 *
 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
 *   The entity being created.
 * @param \Drupal\Core\Field\FieldDefinitionInterface $definition
 *   The field definition.
 *
 * @return array
 *   An array of default value keys with each entry keyed with the “value” key.
 *
 * @see \Drupal\Core\Field\FieldConfigBase::getDefaultValue()
 */
function farm_location_is_location_default_value(ContentEntityInterface $entity, FieldDefinitionInterface $definition) : array {
  $default = FALSE;

  // Load the entity bundle.
  $bundle = \Drupal::service('entity_type.manager')
    ->getStorage('asset_type')
    ->load($entity
    ->bundle());

  // Use the bundle's is_location third-party setting as a default.
  $is_location = $bundle
    ->getThirdPartySetting('farm_location', 'is_location');
  if (!empty($is_location)) {
    $default = TRUE;
  }
  return [
    [
      'value' => $default,
    ],
  ];
}

/**
 * Sets the default value for asset is_fixed boolean field.
 *
 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
 *   The entity being created.
 * @param \Drupal\Core\Field\FieldDefinitionInterface $definition
 *   The field definition.
 *
 * @return array
 *   An array of default value keys with each entry keyed with the “value” key.
 *
 * @see \Drupal\Core\Field\FieldConfigBase::getDefaultValue()
 */
function farm_location_is_fixed_default_value(ContentEntityInterface $entity, FieldDefinitionInterface $definition) : array {
  $default = FALSE;

  // Load the entity bundle.
  $bundle = \Drupal::service('entity_type.manager')
    ->getStorage('asset_type')
    ->load($entity
    ->bundle());

  // Use the bundle's is_fixed third-party setting as a default.
  $is_fixed = $bundle
    ->getThirdPartySetting('farm_location', 'is_fixed');
  if (!empty($is_fixed)) {
    $default = TRUE;
  }
  return [
    [
      'value' => $default,
    ],
  ];
}

/**
 * Sets the default value for log movement boolean field.
 *
 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
 *   The entity being created.
 * @param \Drupal\Core\Field\FieldDefinitionInterface $definition
 *   The field definition.
 *
 * @return array
 *   An array of default value keys with each entry keyed with the “value” key.
 *
 * @see \Drupal\Core\Field\FieldConfigBase::getDefaultValue()
 */
function farm_location_is_movement_default_value(ContentEntityInterface $entity, FieldDefinitionInterface $definition) : array {
  $default = FALSE;

  // Load the entity bundle.
  $bundle = \Drupal::service('entity_type.manager')
    ->getStorage('log_type')
    ->load($entity
    ->bundle());

  // Use the bundle's is_movement third-party setting as a default.
  $is_movement = $bundle
    ->getThirdPartySetting('farm_location', 'is_movement');
  if (!empty($is_movement)) {
    $default = TRUE;
  }
  return [
    [
      'value' => $default,
    ],
  ];
}

Functions

Namesort descending Description
farm_location_entity_base_field_info Implements hook_entity_base_field_info().
farm_location_form_asset_form_alter Implements hook_form_BASE_FORM_ID_alter().
farm_location_is_fixed_default_value Sets the default value for asset is_fixed boolean field.
farm_location_is_location_default_value Sets the default value for asset is_location boolean field.
farm_location_is_movement_default_value Sets the default value for log movement boolean field.