<?php

/**
 * @file
 * Alter wrapping markup of fields.
 */

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\fences\TagManagerInterface;

/**
 * Implements hook_theme_registry_alter().
 */
function fences_theme_registry_alter(&$theme_registry) {
  $module_path = drupal_get_path('module', 'fences');
  $theme_registry['field']['path'] = $module_path;
}

/**
 * Implements hook_field_formatter_third_party_settings_form().
 */
function fences_field_formatter_third_party_settings_form(FormatterInterface $plugin, FieldDefinitionInterface $field_definition, $view_mode, $form, FormStateInterface $form_state) {
  $settings['fences'] = [];

  $invisible_on_none = function ($name) {
    return [
      'invisible' => [
        '[name$="' . $name . ']"]' => [
          'value' => TagManagerInterface::NO_MARKUP_VALUE,
        ]
      ]
    ];
  };

  $tag_options = \Drupal::service('fences.tag_manager')->getTagOptions();
  $settings['fences'] = [
    '#type' => 'fieldset',
    '#title' => t('Fences'),
    '#collapsed' => TRUE,
    '#collapsible' => TRUE,
  ];
  $settings['fences']['fences_field_tag'] = [
    '#title' => t('Field Tag'),
    '#type' => 'select',
    '#options' => $tag_options,
    '#default_value' => $plugin->getThirdPartySetting('fences', 'fences_field_tag', 'div'),
  ];
  $settings['fences']['fences_field_classes'] = [
    '#title' => t('Field Classes'),
    '#type' => 'textfield',
    '#default_value' => $plugin->getThirdPartySetting('fences', 'fences_field_classes'),
    '#states' => $invisible_on_none('fences_field_tag'),
  ];
  $settings['fences']['fences_field_item_tag'] = [
    '#title' => t('Field Item Tag'),
    '#type' => 'select',
    '#options' => $tag_options,
    '#default_value' => $plugin->getThirdPartySetting('fences', 'fences_field_item_tag', 'div'),
  ];
  $settings['fences']['fences_field_item_classes'] = [
    '#title' => t('Field Item Classes'),
    '#type' => 'textfield',
    '#default_value' => $plugin->getThirdPartySetting('fences', 'fences_field_item_classes'),
    '#states' => $invisible_on_none('fences_field_item_tag'),
  ];
  $settings['fences']['fences_label_tag'] = [
    '#title' => t('Label Tag'),
    '#type' => 'select',
    '#options' => $tag_options,
    '#default_value' => $plugin->getThirdPartySetting('fences', 'fences_label_tag', 'div'),
  ];
  $settings['fences']['fences_label_classes'] = [
    '#title' => t('Label Classes'),
    '#type' => 'textfield',
    '#default_value' => $plugin->getThirdPartySetting('fences', 'fences_label_classes'),
    '#states' => $invisible_on_none('fences_label_tag'),
  ];
  return $settings['fences'];
}

/**
 * Implememts hook_preprocess_field().
 */
function fences_preprocess_field(&$vars) {

  $entity_type = $vars['element']['#object']->getEntityTypeId();
  $bundle = $vars['element']['#object']->bundle();
  $display = entity_get_display($entity_type, $bundle, $vars['element']['#view_mode']);

  // If the EntityViewDisplay is new then they're using the default display so
  // lets grab that instead.
  if ($display->isNew()) {
    $display = entity_get_display($entity_type, $bundle, 'default');
  }

  // Default to showing all markup.
  $vars['display_label_tag'] = TRUE;
  $vars['display_field_tag'] = TRUE;
  $vars['display_item_tag'] = TRUE;

  $field_formatter_config = $display->getComponent($vars['field_name']);
  if (isset($field_formatter_config['third_party_settings']['fences'])) {
    $fences_config = $field_formatter_config['third_party_settings']['fences'];

    $vars['display_label_tag'] = $fences_config['fences_label_tag'] !== TagManagerInterface::NO_MARKUP_VALUE;
    $vars['display_field_tag'] = $fences_config['fences_field_tag'] !== TagManagerInterface::NO_MARKUP_VALUE;
    $vars['display_item_tag'] = $fences_config['fences_field_item_tag'] !== TagManagerInterface::NO_MARKUP_VALUE;

    $vars['label_tag'] = $fences_config['fences_label_tag'];
    $vars['field_tag'] = $fences_config['fences_field_tag'];
    $vars['field_item_tag'] = $fences_config['fences_field_item_tag'];

    $vars['attributes']['class'][] = $fences_config['fences_field_classes'];
    $vars['title_attributes']['class'][] = $fences_config['fences_label_classes'];

    if (!empty($fences_config['fences_field_item_classes'])) {
      foreach ($vars['items'] as $field_item) {
        $field_item['attributes']->addClass($fences_config['fences_field_item_classes']);
      }
    }
  }
}
