farm-2.x-dev/modules/core/location/farm_location.module

modules/core/location/farm_location.module
<?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) {
  \Drupal::moduleHandler()->loadInclude('farm_location', 'inc', '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 [];
  }
}

/**
 * Implements hook_entity_base_field_info_alter().
 */
function farm_location_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  /** @var \Drupal\field\Entity\FieldConfig[] $fields */

  // Prevent creating circular asset location.
  if ($entity_type->id() == 'log' && !empty($fields['asset'])) {
    $fields['asset']->addConstraint('CircularAssetLocation');
  }
}

/**
 * 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],
  ];
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc