<?php

/**
 * @file
 * Contains farm_entity_fields.module.
 */

use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Implements hook_entity_base_field_info().
 */
function farm_entity_fields_entity_base_field_info(EntityTypeInterface $entity_type) {

  // Include helper functions.
  \Drupal::moduleHandler()->loadInclude('farm_entity_fields', 'inc', 'farm_entity_fields.base_fields');

  // Add common base fields to all asset types.
  if ($entity_type->id() == 'asset') {
    return farm_entity_fields_asset_base_fields();
  }

  // Add common base fields to all log types.
  elseif ($entity_type->id() == 'log') {
    return farm_entity_fields_log_base_fields();
  }

  // Add common base fields to all plan types.
  elseif ($entity_type->id() == 'plan') {
    return farm_entity_fields_plan_base_fields();
  }
}

/**
 * Implements hook_entity_base_field_info_alter().
 */
function farm_entity_fields_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {

  // Only alter asset, log, and plan fields.
  if (!in_array($entity_type->id(), ['asset', 'log', 'plan'])) {
    return;
  }

  $alter_fields = [
    'name' => [
      'label' => 'hidden',
      'weight' => -100,
    ],
    'timestamp' => [
      'weight' => -90,
    ],
    'type' => [
      'weight' => -85,
      'hidden' => 'form',
    ],
    'status' => [
      'weight' => -80,
    ],
    'created' => [
      'hidden' => TRUE,
    ],
    'uid' => [
      'hidden' => TRUE,
    ],
  ];
  foreach ($alter_fields as $name => $options) {

    // If the field does not exist on this entity type, skip it.
    if (empty($fields[$name])) {
      continue;
    }

    // Load the form and view display options.
    $form_display_options = $fields[$name]->getDisplayOptions('form');
    $view_display_options = $fields[$name]->getDisplayOptions('view');

    // Set the field weight.
    if (!empty($options['weight'])) {
      $form_display_options['weight'] = $view_display_options['weight'] = $options['weight'];
    }

    // Hide the field, if desired.
    if (!empty($options['hidden'])) {
      if ($options['hidden'] === TRUE || $options['hidden'] === 'form') {
        $form_display_options['region'] = 'hidden';
      }
      if ($options['hidden'] === TRUE || $options['hidden'] === 'view') {
        $view_display_options['region'] = 'hidden';
      }
    }

    // Hide the label, if desired.
    if (!empty($options['label']) && $options['label'] == 'hidden') {
      $view_display_options['label'] = 'hidden';
    }

    // Otherwise, set the label to inline.
    else {
      $view_display_options['label'] = 'inline';
    }

    switch ($name) {

      // Change state field from transition form to default.
      case 'status':
        $view_display_options['type'] = 'list_default';
        break;

      // Don't display a link to the entity type reference.
      case 'type':
        $view_display_options['settings']['link'] = FALSE;
        break;
    }

    // Save the options.
    $fields[$name]->setDisplayOptions('form', $form_display_options);
    $fields[$name]->setDisplayOptions('view', $view_display_options);
  }
}
