<?php

/**
 * @file
 * Make base fields such as 'title' available in "Manage Display".
 */

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_entity_type_build().
 */
function manage_display_entity_type_build(array &$entity_types) {
  $attributes = [
    'enable_base_field_custom_preprocess_skipping' => ['node', 'taxonomy_term', 'aggregator_feed', 'aggregator_item'],
    'enable_page_title_template' => ['node', 'taxonomy_term', 'aggregator_feed', 'media'],
  ];

  foreach ($attributes as $property => $types) {
    foreach ($types as $type) {
      if (isset($entity_types[$type])) {
        $entity_types[$type]->set($property, TRUE);
      }
    }
  }

  if (isset($entity_types['user'])) {
    // Set user name as a label else it gets printed twice on the user page.
    $keys = $entity_types['user']->get('entity_keys');
    $keys['label'] = 'name';
    $keys = $entity_types['user']->set('entity_keys', $keys);
  }
}

/**
 * Implements hook_entity_base_field_info_alter().
 */
function manage_display_entity_base_field_info_alter(&$base_field_definitions, EntityTypeInterface $entity_type) {
  $info = manage_display_base_field_info($entity_type->id());
  foreach ($info as $field => $options) {
    $base_field_definitions[$field]
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayOptions('view', $options);
  }
}

/**
 * Return information about the base fields that can be managed.
 *
 * @param string $entity_type_id
 *   Entity type ID to return fields for.
 *
 * @return array
 *   Array keyed by field name with value equal to the default display options.
 */
function manage_display_base_field_info($entity_type_id) {
  // Node.
  // - Default uid and created to hidden as that's most often correct,
  //   especially on teaser.
  $info['node']['title'] = ['label' => 'hidden', 'type' => 'title', 'weight' => -50];
  $info['node']['created'] = ['region' => 'hidden'];
  $info['node']['uid'] = ['region' => 'hidden'];

  // User.
  // - Default user name to hidden to match Drupal default.
  $info['user']['name'] = ['region' => 'hidden'];

  // Taxonomy term.
  $info['taxonomy_term']['name'] = $info['node']['title'];

  // Aggregator feed.
  // - @todo Create new formatter for 'image' that displays the URI as an <img>
  //   tag with appropriate title.
  $info['aggregator_feed']['title'] = $info['node']['title'];
  $info['aggregator_feed']['image'] = ['type' => 'uri_link', 'label' => 'hidden', 'weight' => 2];
  $info['aggregator_feed']['description'] = ['type' => 'aggregator_xss', 'label' => 'hidden', 'weight' => 3];

  // Aggregator item.
  $info['aggregator_item']['title'] = $info['node']['title'];
  $info['aggregator_item']['title']['settings']['tag'] = 'h3';
  $info['aggregator_item']['description'] = ['type' => 'aggregator_xss', 'label' => 'hidden', 'weight' => 2];

  return isset($info[$entity_type_id]) ? $info[$entity_type_id] : [];
}

/**
 * Implements hook_theme_registry_alter().
 */
function manage_display_theme_registry_alter(&$theme_registry) {
  // Disable the 'inline' versions of node base field templates to workaround
  // https://www.drupal.org/node/2993647.
  unset($theme_registry['field__node__title']);
  unset($theme_registry['field__node__uid']);
}

/**
 * Implements hook_theme().
 */
function manage_display_theme() {
  return [
    'submitted' => [
      'base hook' => 'username',
      'variables' => [
        'date' => NULL,
        'user_picture' => NULL,
        'metadata' => [],
        'account' => NULL,
        'attributes' => [],
        'link_options' => [],
      ],
    ],
  ];
}

/**
 * Prepares variables for 'submitted' field formatter template.
 */
function template_preprocess_submitted(&$variables) {
  $variables['date'] = \Drupal::service('date.formatter')->format($variables['date']);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function manage_display_form_node_type_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Hide display_submitted in GUI.
  $form['display']['#access'] = FALSE;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function manage_display_form_system_theme_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Hide the theme settings for user_pictures because the submitted formatter
  // has its own setting.
  $form['theme_settings']['toggle_node_user_picture']['#access'] = FALSE;
  //$form['theme_settings']['toggle_comment_user_picture']['#access'] = FALSE;
}
