You are here

extra_field_description.module in Extra Field Description 8

Same filename and directory in other branches
  1. 7 extra_field_description.module

Main module functions for the extra_field_description module.

File

extra_field_description.module
View source
<?php

/**
 * @file
 * Main module functions for the extra_field_description module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Field\FieldDefinitionInterface;

/**
 * Implements hook_help().
 */
function extra_field_description_help($path, $arg) {
  switch ($path) {
    case 'help.page.extra_field_description':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module give us opportunity to append extra description to the field.') . '</p>';
      $output .= '<p>' . t('In almost all themes extra description located below the field.') . '</p>';
      $output .= '<p>' . t('Sometimes needed to append extra description above the field, see attached example.') . '</p>';
      $output .= '<p>' . t('Module "Extra field description" is the best solution.') . '</p>';
      $output .= '<p>' . t('For more information, see the <a href="https://www.drupal.org/project/extra_field_description">Extra Field Description module</a>.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_field_widget_third_party_settings_form().
 */
function extra_field_description_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
  if ($field_definition
    ->getFieldStorageDefinition()
    ->isBaseField() == FALSE) {
    $plugin_third_party_settings = $plugin
      ->getThirdPartySettings('extra_field_description');

    // Check if we have access to extra fields.
    if (\Drupal::currentUser()
      ->hasPermission('administer field prefix')) {

      // Append extra fields in field settings form.
      $form['extra_description'] = [
        '#type' => 'details',
        '#title' => t("Extra description settings"),
        '#weight' => -4,
        '#open' => TRUE,
      ];
      $form['extra_description']['over_description'] = [
        '#type' => 'textarea',
        '#title' => t('Extra description'),
        '#description' => t("Extra description over the field.<br />Allowed HTML tags: @tags", [
          '@tags' => FieldFilteredMarkup::displayAllowedTags(),
        ]),
        '#default_value' => isset($plugin_third_party_settings['extra_description']['over_description']) ? t($plugin_third_party_settings['extra_description']['over_description']) : '',
      ];
      $plugin
        ->setThirdPartySetting('extra_field_description', 'extra_description', $form['extra_description']);
      return $form;
    }
  }
}

/**
 * Implements hook_field_widget_form_alter().
 */
function extra_field_description_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  $third_party_settings = $context['widget']
    ->getThirdPartySettings('extra_field_description', 'extra_description');
  if (!empty($third_party_settings)) {
    if (isset($element['value'])) {
      if ($element['value']['#type'] == 'datetime' || $element['value']['#type'] == 'datelist') {
        $element['#field_prefix'] = '<div class="extra-description" >' . $third_party_settings['extra_description']['over_description'] . '</div>';
      }
      else {
        $element['value']['#field_prefix'] = '<div class="extra-description" >' . $third_party_settings['extra_description']['over_description'] . '</div>';
      }
    }
    elseif (isset($element['target_id'])) {
      $element['target_id']['#field_prefix'] = '<div class="extra-description" >' . $third_party_settings['extra_description']['over_description'] . '</div>';
    }
    else {
      $element['#field_prefix'] = '<div class="extra-description" >' . $third_party_settings['extra_description']['over_description'] . '</div>';
    }
  }
}

/**
 * Implements hook_page_attachments().
 */
function extra_field_description_page_attachments(array &$attachments) {
  $attachments['#attached']['library'][] = 'extra_field_description/extra_field_description_css';
}