You are here

faqfield.module in FAQ Field 2.0.x

Same filename and directory in other branches
  1. 8 faqfield.module
  2. 7 faqfield.module

Defines a field for frequently asked questions.

File

faqfield.module
View source
<?php

/**
 * @file
 * Defines a field for frequently asked questions.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Render\Markup;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function faqfield_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.faqfield':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The FAQ Field module defines a field for storing frequently asked questions, for use with the Field module. See the <a href=":field_ui">Field module help page</a> for more information about fields.', [
        ':field_ui' => \Drupal::moduleHandler()
          ->moduleExists('field_ui') ? Url::fromRoute('help.page', [
          'name' => 'field_ui',
        ])
          ->toString() : '#',
      ]) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_theme().
 */
function faqfield_theme() {
  return [
    // Simple text field formatter template.
    'faqfield_simple_text_formatter' => [
      'variables' => [
        'question' => NULL,
        'answer' => NULL,
        'answer_format' => NULL,
        'delta' => NULL,
      ],
      'template' => 'faqfield-simple-text-formatter',
    ],
    // Definition list field formatter template.
    'faqfield_definition_list_formatter' => [
      'variables' => [
        'items' => [],
      ],
      'template' => 'faqfield-definition-list-formatter',
    ],
    // Anchor list field formatter template.
    'faqfield_anchor_list_formatter' => [
      'variables' => [
        'items' => [],
        'list_type' => 'ul',
      ],
      'template' => 'faqfield-anchor-list-formatter',
    ],
    // jQuery Accordion field formatter template.
    'faqfield_jquery_accordion_formatter' => [
      'variables' => [
        'items' => [],
        'id' => NULL,
      ],
      'template' => 'faqfield-jquery-accordion-formatter',
    ],
    // Details field formatter template.
    'faqfield_details_formatter' => [
      'variables' => [
        'question' => NULL,
        'answer' => NULL,
        'answer_format' => NULL,
        'delta' => NULL,
      ],
      'template' => 'faqfield-details-formatter',
    ],
  ];
}

/**
 * Prepares variables for the simple text field formatter template.
 *
 * @param array $variables
 *   An associative array containing:
 *   - question: Prefiltered question value by strip_tags.
 *   - answer: Answer value.
 *   - answer_format: Answer markup format.
 *   - delta: Delta of field element.
 */
function template_preprocess_faqfield_simple_text_formatter(array &$variables) {
  $variables['question'] = Markup::create(trim(strip_tags($variables['question'])));
  $variables['answer'] = check_markup($variables['answer'], $variables['answer_format']);
}

/**
 * Prepares variables for the details field formatter template.
 *
 * @param array $variables
 *   An associative array containing:
 *   - question: fields.
 *    - question: Prefiltered question value by strip_tags.
 *   - answer: Answer value.
 *   - answer_format: Answer markup format.
 *   - delta: Delta of field element.
 */
function template_preprocess_faqfield_details_formatter(array &$variables) {
  $variables['question'] = Markup::create(trim(strip_tags($variables['question'])));
  $variables['answer'] = check_markup($variables['answer'], $variables['answer_format']);
}

/**
 * Prepares variables for the definition list field formatter template.
 *
 * @param array $variables
 *   An associative array containing:
 *   - items: An array of objects with the following fields.
 *    - question: Prefiltered question value by strip_tags.
 *    - answer: Prefiltered answer value by field setting format.
 *    - answer_format: Answer markup format.
 */
function template_preprocess_faqfield_definition_list_formatter(array &$variables) {
  foreach ($variables['items'] as &$item) {
    $item['question'] = Markup::create(trim(strip_tags($item['question'])));
    $item['answer'] = check_markup($item['answer'], $item['answer_format']);
    unset($item);
  }
}

/**
 * Prepares variables for the definition list field formatter template.
 *
 * @param array $variables
 *   An associative array containing:
 *   - items: An array of objects with the following fields.
 *    - question: Prefiltered question value by strip_tags.
 *    - answer: Prefiltered answer value by field setting format.
 *    - answer_format: Answer markup format.
 *    - name: String to be used as anchor name.
 *   - list_type: The type of HTML list used for the anchor link list.
 */
function template_preprocess_faqfield_anchor_list_formatter(array &$variables) {
  foreach ($variables['items'] as &$item) {

    // Build string to be used as anchor name.
    $item['name'] = 'faq-' . Html::escape(str_replace(' ', '-', $item['question']));
    $item['question'] = Markup::create(trim(strip_tags($item['question'])));
    $item['answer'] = check_markup($item['answer'], $item['answer_format']);
    unset($item);
  }
}

/**
 * Prepares variables for the jQuery accordion field formatter template.
 *
 * @param array $variables
 *   An associative array containing:
 *   - items: An array of objects with the following fields.
 *    - question: Prefiltered question value by strip_tags.
 *    - answer: Prefiltered answer value by field setting format.
 *    - answer_format: Answer markup format.
 *   - id: Id to wrap the accordion markup in.
 */
function template_preprocess_faqfield_jquery_accordion_formatter(array &$variables) {
  foreach ($variables['items'] as &$item) {
    $item['question'] = Markup::create(trim(strip_tags($item['question'])));
    $item['answer'] = check_markup($item['answer'], $item['answer_format']);
    unset($item);
  }
}

/**
 * Implements hook_field_storage_config_create().
 *
 * Set the default cardinality to unlimited after a faqfield was created.
 */
function faqfield_field_storage_config_create(FieldStorageConfigInterface $field_storage) {
  if ($field_storage
    ->getType() == 'faqfield') {
    $field_storage
      ->setCardinality(FieldStorageConfigInterface::CARDINALITY_UNLIMITED);
  }
}

Functions

Namesort descending Description
faqfield_field_storage_config_create Implements hook_field_storage_config_create().
faqfield_help Implements hook_help().
faqfield_theme Implements hook_theme().
template_preprocess_faqfield_anchor_list_formatter Prepares variables for the definition list field formatter template.
template_preprocess_faqfield_definition_list_formatter Prepares variables for the definition list field formatter template.
template_preprocess_faqfield_details_formatter Prepares variables for the details field formatter template.
template_preprocess_faqfield_jquery_accordion_formatter Prepares variables for the jQuery accordion field formatter template.
template_preprocess_faqfield_simple_text_formatter Prepares variables for the simple text field formatter template.