field_test.module

Same filename in other branches
  1. 7.x modules/field/tests/field_test.module
  2. 9 core/modules/field/tests/modules/field_test/field_test.module
  3. 8.9.x core/modules/field/tests/modules/field_test/field_test.module
  4. 10 core/modules/field/tests/modules/field_test/field_test.module

File

core/modules/field/tests/modules/field_test/field_test.module

View source
<?php


/**
 * @file
 * Helper module for the Field API tests.
 *
 * The module defines
 * - a field type and its formatters and widgets (field_test.field.inc)
 * - a field storage backend (field_test.storage.inc)
 *
 * The main field_test.module file implements generic hooks and provides some
 * test helper functions
 */
declare (strict_types=1);
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\field\FieldStorageConfigInterface;
require_once __DIR__ . '/field_test.entity.inc';
require_once __DIR__ . '/field_test.field.inc';

/**
 * Store and retrieve keyed data for later verification by unit tests.
 *
 * This function is a simple in-memory key-value store with the
 * distinction that it stores all values for a given key instead of
 * just the most recently set value. field_test module hooks call
 * this function to record their arguments, keyed by hook name. The
 * unit tests later call this function to verify that the correct
 * hooks were called and were passed the correct arguments.
 *
 * This function ignores all calls until the first time it is called
 * with $key of NULL. Each time it is called with $key of NULL, it
 * erases all previously stored data from its internal cache, but also
 * returns the previously stored data to the caller. A typical usage
 * scenario is:
 *
 * @code
 *   // calls to field_test_memorize() here are ignored
 *
 *   // turn on memorization
 *   field_test_memorize();
 *
 *   // call some Field API functions that invoke field_test hooks
 *   FieldStorageConfig::create($field_definition)->save();
 *
 *   // retrieve and reset the memorized hook call data
 *   $mem = field_test_memorize();
 *
 *   // make sure hook_field_storage_config_create() is invoked correctly
 *   assertEquals(1, count($mem['field_test_field_storage_config_create']));
 *   assertEquals([$field], $mem['field_test_field_storage_config_create'][0]);
 * @endcode
 *
 * @param $key
 *   The key under which to store to $value, or NULL as described above.
 * @param $value
 *   A value to store for $key.
 *
 * @return array|null
 *   An array mapping each $key to an array of each $value passed in
 *   for that key.
 */
function field_test_memorize($key = NULL, $value = NULL) {
    static $memorize;
    if (!isset($key)) {
        $return = $memorize;
        $memorize = [];
        return $return;
    }
    if (is_array($memorize)) {
        $memorize[$key][] = $value;
    }
}

/**
 * Memorize calls to field_test_field_storage_config_create().
 */
function field_test_field_storage_config_create(FieldStorageConfigInterface $field_storage) {
    $args = func_get_args();
    field_test_memorize(__FUNCTION__, $args);
}

/**
 * Sets up alterations for widget alter tests.
 *
 * @see \Drupal\field\Tests\FormTest::widgetAlterTest()
 */
function _field_test_alter_widget($hook, array &$field_widget_complete_form, FormStateInterface $form_state, array $context) {
    $elements =& $field_widget_complete_form['widget'];
    // Set a message if this is for the form displayed to set default value for
    // the field.
    if ($context['default']) {
        \Drupal::messenger()->addStatus("From {$hook}(): Default form is true.");
    }
    $alter_info = \Drupal::state()->get("field_test.widget_alter_test");
    $name = $context['items']->getFieldDefinition()
        ->getName();
    if (!empty($alter_info) && $hook === $alter_info['hook'] && $name === $alter_info['field_name']) {
        $elements['#prefix'] = "From {$hook}(): prefix on {$name} parent element.";
        foreach (Element::children($elements) as $delta => $element) {
            $elements[$delta]['#suffix'] = "From {$hook}(): suffix on {$name} child element.";
        }
    }
}
function field_test_entity_reference_selection_alter(array &$definitions) : void {
    if (\Drupal::state()->get('field_test_disable_broken_entity_reference_handler')) {
        unset($definitions['broken']);
    }
}

Functions

Title Deprecated Summary
field_test_entity_reference_selection_alter
field_test_field_storage_config_create Memorize calls to field_test_field_storage_config_create().
field_test_memorize Store and retrieve keyed data for later verification by unit tests.
_field_test_alter_widget Sets up alterations for widget alter tests.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.