Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
|
@ -0,0 +1,7 @@
|
|||
field.formatter.settings.field_plugins_test_text_formatter:
|
||||
type: field.formatter.settings.text_trimmed
|
||||
label: 'Test text formatter display format settings'
|
||||
|
||||
field.widget.settings.field_plugins_test_text_widget:
|
||||
type: field.widget.settings.text_textfield
|
||||
label: 'Test text field widget settings'
|
|
@ -0,0 +1,8 @@
|
|||
name: 'Field Plugins Test'
|
||||
type: module
|
||||
description: 'Support module for the field and entity display tests.'
|
||||
core: 8.x
|
||||
package: Testing
|
||||
version: VERSION
|
||||
dependencies:
|
||||
- text
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_plugins_test\Plugin\Field\FieldFormatter\TestTextTrimmedFormatter.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_plugins_test\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\text\Plugin\Field\FieldFormatter\TextTrimmedFormatter;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'field_plugins_test_text_formatter' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "field_plugins_test_text_formatter",
|
||||
* label = @Translation("Test Trimmed"),
|
||||
* field_types = {
|
||||
* "text",
|
||||
* "text_long",
|
||||
* "text_with_summary"
|
||||
* },
|
||||
* quickedit = {
|
||||
* "editor" = "form"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class TestTextTrimmedFormatter extends TextTrimmedFormatter {
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_plugins_test\Plugin\Field\FieldWidget\TestTextfieldWidget.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_plugins_test\Plugin\Field\FieldWidget;
|
||||
|
||||
use Drupal\text\Plugin\Field\FieldWidget\TextfieldWidget;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'field_plugins_test_text_widget' widget.
|
||||
*
|
||||
* @FieldWidget(
|
||||
* id = "field_plugins_test_text_widget",
|
||||
* label = @Translation("Test Text field"),
|
||||
* field_types = {
|
||||
* "text",
|
||||
* "string"
|
||||
* },
|
||||
* )
|
||||
*/
|
||||
class TestTextfieldWidget extends TextfieldWidget {
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
field.formatter.settings.field_test_default:
|
||||
type: mapping
|
||||
label: 'Field test default display format settings'
|
||||
mapping:
|
||||
test_formatter_setting:
|
||||
type: string
|
||||
label: 'Test setting'
|
||||
|
||||
field.formatter.settings.field_test_multiple:
|
||||
type: mapping
|
||||
label: 'Multiple field test display format settings'
|
||||
mapping:
|
||||
test_formatter_setting_multiple:
|
||||
type: string
|
||||
label: 'Test setting'
|
||||
alter:
|
||||
type: boolean
|
||||
label: 'Test altering'
|
||||
|
||||
field.formatter.settings.field_empty_setting:
|
||||
type: mapping
|
||||
label: 'Empty setting field display format settings'
|
||||
mapping:
|
||||
field_empty_setting:
|
||||
type: string
|
||||
label: 'Test setting'
|
||||
|
||||
field.formatter.settings.field_test_with_prepare_view:
|
||||
type: mapping
|
||||
label: 'Field prepare step display format settings'
|
||||
mapping:
|
||||
test_formatter_setting_additional:
|
||||
type: string
|
||||
label: 'Test setting'
|
||||
|
||||
field.widget.settings.test_field_widget:
|
||||
type: mapping
|
||||
label: 'Test field widget settings'
|
||||
mapping:
|
||||
test_widget_setting:
|
||||
type: string
|
||||
label: 'Test setting'
|
||||
|
||||
field.widget.settings.test_field_widget_multiple:
|
||||
type: mapping
|
||||
label: 'Test multiple field widget settings'
|
||||
mapping:
|
||||
test_widget_setting_multiple:
|
||||
type: string
|
||||
label: 'Test setting'
|
||||
|
||||
field.storage_settings.test_field:
|
||||
type: mapping
|
||||
label: 'Test field storage settings'
|
||||
mapping:
|
||||
test_field_storage_setting:
|
||||
type: string
|
||||
label: 'Test field storage setting'
|
||||
changeable:
|
||||
type: string
|
||||
label: 'A changeable field storage setting'
|
||||
unchangeable:
|
||||
type: string
|
||||
label: 'An unchangeable field storage setting'
|
||||
config_data_from_storage_setting:
|
||||
type: boolean
|
||||
label: 'Test FieldItemInterface::storageSettingsToConfigData()'
|
||||
|
||||
field.storage_settings.test_field_with_dependencies:
|
||||
type: field.storage_settings.test_field
|
||||
label: 'Test field with dependencies storage settings'
|
||||
|
||||
field.storage_settings.hidden_test_field:
|
||||
type: field.storage_settings.test_field
|
||||
label: 'Hidden test field storage settings'
|
||||
|
||||
field.storage_settings.test_field_with_preconfigured_options:
|
||||
type: field.storage_settings.test_field
|
||||
label: 'Test field with preconfigured options storage settings'
|
||||
|
||||
field.field_settings.test_field:
|
||||
type: mapping
|
||||
label: 'Test field field settings'
|
||||
mapping:
|
||||
test_field_setting:
|
||||
type: string
|
||||
label: 'Test field setting'
|
||||
config_data_from_field_setting:
|
||||
type: boolean
|
||||
label: 'Test FieldItemInterface::fieldSettingsToConfigData()'
|
||||
|
||||
field.field_settings.test_field_with_dependencies:
|
||||
type: field.field_settings.test_field
|
||||
label: 'Test field with dependencies field settings'
|
||||
|
||||
field.field_settings.hidden_test_field:
|
||||
type: field.field_settings.test_field
|
||||
label: 'Hidden test field field settings'
|
||||
|
||||
field.field_settings.test_field_with_preconfigured_options:
|
||||
type: field.field_settings.test_field
|
||||
label: 'Test field with preconfigured settings'
|
||||
|
||||
field.value.test_field:
|
||||
type: mapping
|
||||
label: 'Default value'
|
||||
mapping:
|
||||
value:
|
||||
type: label
|
||||
label: 'Value'
|
||||
|
||||
|
||||
field.formatter.third_party.field_test:
|
||||
type: mapping
|
||||
label: 'Field test entity display third party setting'
|
||||
mapping:
|
||||
foo:
|
||||
type: string
|
||||
label: 'Test setting'
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Defines an entity type.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_entity_type_alter().
|
||||
*/
|
||||
function field_test_entity_type_alter(array &$entity_types) {
|
||||
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
|
||||
foreach (field_test_entity_info_translatable() as $entity_type => $translatable) {
|
||||
$entity_types[$entity_type]->set('translatable', $translatable);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to enable entity translations.
|
||||
*/
|
||||
function field_test_entity_info_translatable($entity_type_id = NULL, $translatable = NULL) {
|
||||
$stored_value = &drupal_static(__FUNCTION__, array());
|
||||
if (isset($entity_type_id)) {
|
||||
$entity_manager = \Drupal::entityManager();
|
||||
$original = $entity_manager->getDefinition($entity_type_id);
|
||||
$stored_value[$entity_type_id] = $translatable;
|
||||
if ($translatable != $original->isTranslatable()) {
|
||||
$entity_manager->clearCachedDefinitions();
|
||||
$entity_type = $entity_manager->getDefinition($entity_type_id);
|
||||
$entity_manager->onEntityTypeUpdate($entity_type, $original);
|
||||
}
|
||||
}
|
||||
return $stored_value;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Defines a field type and its formatters and widgets.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\field\FieldStorageConfigInterface;
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_info_alter().
|
||||
*/
|
||||
function field_test_field_widget_info_alter(&$info) {
|
||||
$info['test_field_widget_multiple']['field_types'][] = 'test_field';
|
||||
$info['test_field_widget_multiple']['field_types'][] = 'test_field_with_preconfigured_options';
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_storage_config_update_forbid().
|
||||
*/
|
||||
function field_test_field_storage_config_update_forbid(FieldStorageConfigInterface $field_storage, FieldStorageConfigInterface $prior_field_storage) {
|
||||
if ($field_storage->getType() == 'test_field' && $field_storage->getSetting('unchangeable') != $prior_field_storage->getSetting('unchangeable')) {
|
||||
throw new FieldStorageDefinitionUpdateForbiddenException("field_test 'unchangeable' setting cannot be changed'");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sample 'default value' callback.
|
||||
*/
|
||||
function field_test_default_value(FieldableEntityInterface $entity, FieldDefinitionInterface $definition) {
|
||||
return array(array('value' => 99));
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_field_access().
|
||||
*/
|
||||
function field_test_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
|
||||
if ($field_definition->getName() == "field_no_{$operation}_access") {
|
||||
return AccessResult::forbidden();
|
||||
}
|
||||
|
||||
// Only grant view access to test_view_field fields when the user has
|
||||
// 'view test_view_field content' permission.
|
||||
if ($field_definition->getName() == 'test_view_field' && $operation == 'view') {
|
||||
return AccessResult::forbiddenIf(!$account->hasPermission('view test_view_field content'))->cachePerPermissions();
|
||||
}
|
||||
|
||||
return AccessResult::allowed();
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
name: 'Field API Test'
|
||||
type: module
|
||||
description: 'Support module for the Field API tests.'
|
||||
core: 8.x
|
||||
package: Testing
|
||||
version: VERSION
|
||||
dependencies:
|
||||
- entity_test
|
170
core/modules/field/tests/modules/field_test/field_test.module
Normal file
170
core/modules/field/tests/modules/field_test/field_test.module
Normal file
|
@ -0,0 +1,170 @@
|
|||
<?php
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\field\FieldStorageConfigInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Helper module for the Field API tests.
|
||||
*
|
||||
* The module defines
|
||||
* - an entity type (field_test.entity.inc)
|
||||
* - 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
|
||||
*/
|
||||
|
||||
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
|
||||
* entity_create('field_storage_config', $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
|
||||
* assertEqual(count($mem['field_test_field_storage_config_create']), 1);
|
||||
* assertEqual($mem['field_test_field_storage_config_create'][0], array($field));
|
||||
* @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
|
||||
* An array mapping each $key to an array of each $value passed in
|
||||
* for that key.
|
||||
*/
|
||||
function field_test_memorize($key = NULL, $value = NULL) {
|
||||
$memorize = &drupal_static(__FUNCTION__, NULL);
|
||||
|
||||
if (!isset($key)) {
|
||||
$return = $memorize;
|
||||
$memorize = array();
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_display_build_alter().
|
||||
*/
|
||||
function field_test_entity_display_build_alter(&$output, $context) {
|
||||
$display_options = $context['display']->getComponent('test_field');
|
||||
if (isset($display_options['settings']['alter'])) {
|
||||
$output['test_field'][] = array('#markup' => 'field_test_entity_display_build_alter');
|
||||
}
|
||||
|
||||
if (isset($output['test_field'])) {
|
||||
$output['test_field'][] = array('#markup' => 'entity language is ' . $context['entity']->language()->getId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_form_alter().
|
||||
*/
|
||||
function field_test_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
|
||||
$field_definition = $context['items']->getFieldDefinition();
|
||||
switch ($field_definition->getName()) {
|
||||
case 'alter_test_text':
|
||||
drupal_set_message('Field size: ' . $context['widget']->getSetting('size'));
|
||||
break;
|
||||
|
||||
case 'alter_test_options':
|
||||
drupal_set_message('Widget type: ' . $context['widget']->getPluginId());
|
||||
break;
|
||||
}
|
||||
// Set a message if this is for the form displayed to set default value for
|
||||
// the field.
|
||||
if ($context['default']) {
|
||||
drupal_set_message('From hook_field_widget_form_alter(): Default form is true.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_query_TAG_alter() for tag 'efq_table_prefixing_test'.
|
||||
*
|
||||
* @see \Drupal\system\Tests\Entity\EntityFieldQueryTest::testTablePrefixing()
|
||||
*/
|
||||
function field_test_query_efq_table_prefixing_test_alter(&$query) {
|
||||
// Add an additional join onto the entity base table. This will cause an
|
||||
// exception if the EFQ does not properly prefix the base table.
|
||||
$query->join('entity_test','et2','%alias.id = entity_test.id');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_query_TAG_alter() for tag 'efq_metadata_test'.
|
||||
*
|
||||
* @see \Drupal\system\Tests\Entity\EntityQueryTest::testMetaData()
|
||||
*/
|
||||
function field_test_query_efq_metadata_test_alter(&$query) {
|
||||
global $efq_test_metadata;
|
||||
$efq_test_metadata = $query->getMetadata('foo');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_extra_field_info_alter().
|
||||
*/
|
||||
function field_test_entity_extra_field_info_alter(&$info) {
|
||||
// Remove all extra fields from the 'no_fields' content type;
|
||||
unset($info['node']['no_fields']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_bundle_field_info_alter().
|
||||
*/
|
||||
function field_test_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
|
||||
if (($field_name = \Drupal::state()->get('field_test_set_constraint', FALSE)) && $entity_type->id() == 'entity_test' && $bundle == 'entity_test' && !empty($fields[$field_name])) {
|
||||
$fields[$field_name]->setPropertyConstraints('value', [
|
||||
'Range' => [
|
||||
'min' => 0,
|
||||
'max' => 32,
|
||||
],
|
||||
]);
|
||||
}
|
||||
if (($field_name = \Drupal::state()->get('field_test_add_constraint', FALSE)) && $entity_type->id() == 'entity_test' && $bundle == 'entity_test' && !empty($fields[$field_name])) {
|
||||
$fields[$field_name]->addPropertyConstraints('value', [
|
||||
'Range' => [
|
||||
'min' => 0,
|
||||
'max' => 32,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
view test_view_field content:
|
||||
title: 'View test field content'
|
||||
description: 'View published test_view_field content.'
|
||||
administer field_test content:
|
||||
title: 'Administer field_test content'
|
||||
description: 'Manage field_test content'
|
|
@ -0,0 +1,13 @@
|
|||
field_test.entity_nested_form:
|
||||
path: '/test-entity/nested/{entity_1}/{entity_2}'
|
||||
defaults:
|
||||
_title: 'Nested entity form'
|
||||
_form: '\Drupal\field_test\Form\NestedEntityTestForm'
|
||||
options:
|
||||
parameters:
|
||||
entity_1:
|
||||
type: 'entity:entity_test'
|
||||
entity_2:
|
||||
type: 'entity:entity_test'
|
||||
requirements:
|
||||
_permission: 'administer entity_test content'
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Form\NestedEntityTestForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Form;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Form\FormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
|
||||
/**
|
||||
* Provides a form for field_test routes.
|
||||
*/
|
||||
class NestedEntityTestForm extends FormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'field_test_entity_nested_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity_1 = NULL, EntityInterface $entity_2 = NULL) {
|
||||
// First entity.
|
||||
$form_state->set('entity_1', $entity_1);
|
||||
$form_display_1 = EntityFormDisplay::collectRenderDisplay($entity_1, 'default');
|
||||
$form_state->set('form_display_1', $form_display_1);
|
||||
$form_display_1->buildForm($entity_1, $form, $form_state);
|
||||
|
||||
// Second entity.
|
||||
$form_state->set('entity_2', $entity_2);
|
||||
$form_display_2 = EntityFormDisplay::collectRenderDisplay($entity_2, 'default');
|
||||
$form_state->set('form_display_2', $form_display_2);
|
||||
$form['entity_2'] = array(
|
||||
'#type' => 'details',
|
||||
'#title' => t('Second entity'),
|
||||
'#tree' => TRUE,
|
||||
'#parents' => array('entity_2'),
|
||||
'#weight' => 50,
|
||||
);
|
||||
|
||||
$form_display_2->buildForm($entity_2, $form['entity_2'], $form_state);
|
||||
|
||||
$form['save'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Save'),
|
||||
'#weight' => 100,
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
$entity_1 = $form_state->get('entity_1');
|
||||
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display_1 */
|
||||
$form_display_1 = $form_state->get('form_display_1');
|
||||
$form_display_1->extractFormValues($entity_1, $form, $form_state);
|
||||
$form_display_1->validateFormValues($entity_1, $form, $form_state);
|
||||
|
||||
$entity_2 = $form_state->get('entity_2');
|
||||
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display_2 */
|
||||
$form_display_2 = $form_state->get('form_display_2');
|
||||
$form_display_2->extractFormValues($entity_2, $form['entity_2'], $form_state);
|
||||
$form_display_2->validateFormValues($entity_2, $form['entity_2'], $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc]
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
/** @var \Drupal\Core\Entity\EntityInterface $entity_1 */
|
||||
$entity_1 = $form_state->get('entity_1');
|
||||
$entity_1->save();
|
||||
|
||||
/** @var \Drupal\Core\Entity\EntityInterface $entity_2 */
|
||||
$entity_2 = $form_state->get('entity_2');
|
||||
$entity_2->save();
|
||||
|
||||
drupal_set_message($this->t('test_entities @id_1 and @id_2 have been updated.', array('@id_1' => $entity_1->id(), '@id_2' => $entity_2->id())));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldFormatter\TestFieldApplicableFormatter.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldFormatter;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'field_test_applicable' formatter.
|
||||
*
|
||||
* It is applicable to test_field fields unless their name is 'deny_applicable'.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "field_test_applicable",
|
||||
* label = @Translation("Applicable"),
|
||||
* description = @Translation("Applicable formatter"),
|
||||
* field_types = {
|
||||
* "test_field"
|
||||
* },
|
||||
* weight = 15,
|
||||
* )
|
||||
*/
|
||||
class TestFieldApplicableFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function isApplicable(FieldDefinitionInterface $field_definition) {
|
||||
return $field_definition->getName() != 'deny_applicable';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items) {
|
||||
return array('#markup' => 'Nothing to see here');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldFormatter\TestFieldDefaultFormatter.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'field_test_default' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "field_test_default",
|
||||
* label = @Translation("Default"),
|
||||
* description = @Translation("Default formatter"),
|
||||
* field_types = {
|
||||
* "test_field",
|
||||
* "test_field_with_preconfigured_options"
|
||||
* },
|
||||
* weight = 1
|
||||
* )
|
||||
*/
|
||||
class TestFieldDefaultFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'test_formatter_setting' => 'dummy test string',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element['test_formatter_setting'] = array(
|
||||
'#title' => t('Setting'),
|
||||
'#type' => 'textfield',
|
||||
'#size' => 20,
|
||||
'#default_value' => $this->getSetting('test_formatter_setting'),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
$summary[] = t('@setting: @value', array('@setting' => 'test_formatter_setting', '@value' => $this->getSetting('test_formatter_setting')));
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items) {
|
||||
$elements = array();
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$elements[$delta] = array('#markup' => $this->getSetting('test_formatter_setting') . '|' . $item->value);
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldFormatter\TestFieldEmptyFormatter.
|
||||
*/
|
||||
namespace Drupal\field_test\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'field_empty_test' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "field_empty_test",
|
||||
* label = @Translation("Field empty test"),
|
||||
* field_types = {
|
||||
* "test_field",
|
||||
* },
|
||||
* weight = -5
|
||||
* )
|
||||
*/
|
||||
class TestFieldEmptyFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'test_empty_string' => '**EMPTY FIELD**',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items) {
|
||||
$elements = array();
|
||||
|
||||
if ($items->isEmpty()) {
|
||||
// For fields with no value, just add the configured "empty" value.
|
||||
$elements[0] = array('#markup' => $this->getSetting('test_empty_string'));
|
||||
}
|
||||
else {
|
||||
foreach ($items as $delta => $item) {
|
||||
// This formatter only needs to output raw for testing.
|
||||
$elements[$delta] = array('#markup' => $item->value);
|
||||
}
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldFormatter\TestFieldEmptySettingFormatter.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'field_empty_setting' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "field_empty_setting",
|
||||
* label = @Translation("Field empty setting"),
|
||||
* field_types = {
|
||||
* "test_field",
|
||||
* },
|
||||
* weight = -1
|
||||
* )
|
||||
*/
|
||||
class TestFieldEmptySettingFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'field_empty_setting' => '',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element['field_empty_setting'] = array(
|
||||
'#title' => t('Setting'),
|
||||
'#type' => 'textfield',
|
||||
'#size' => 20,
|
||||
'#default_value' => $this->getSetting('field_empty_setting'),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
$setting = $this->getSetting('field_empty_setting');
|
||||
if (!empty($setting)) {
|
||||
$summary[] = t('Default empty setting now has a value.');
|
||||
}
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items) {
|
||||
$elements = array();
|
||||
|
||||
if (!empty($items)) {
|
||||
foreach ($items as $delta => $item) {
|
||||
$elements[$delta] = array('#markup' => $this->getSetting('field_empty_setting'));
|
||||
}
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldFormatter\TestFieldMultipleFormatter.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'field_test_multiple' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "field_test_multiple",
|
||||
* label = @Translation("Multiple"),
|
||||
* description = @Translation("Multiple formatter"),
|
||||
* field_types = {
|
||||
* "test_field",
|
||||
* "test_field_with_preconfigured_options"
|
||||
* },
|
||||
* weight = 5
|
||||
* )
|
||||
*/
|
||||
class TestFieldMultipleFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'test_formatter_setting_multiple' => 'dummy test string',
|
||||
'alter' => FALSE,
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element['test_formatter_setting_multiple'] = array(
|
||||
'#title' => t('Setting'),
|
||||
'#type' => 'textfield',
|
||||
'#size' => 20,
|
||||
'#default_value' => $this->getSetting('test_formatter_setting_multiple'),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
$summary[] = t('@setting: @value', array('@setting' => 'test_formatter_setting_multiple', '@value' => $this->getSetting('test_formatter_setting_multiple')));
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items) {
|
||||
$elements = array();
|
||||
|
||||
if (!empty($items)) {
|
||||
$array = array();
|
||||
foreach ($items as $delta => $item) {
|
||||
$array[] = $delta . ':' . $item->value;
|
||||
}
|
||||
$elements[0] = array('#markup' => $this->getSetting('test_formatter_setting_multiple') . '|' . implode('|', $array));
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldFormatter\TestFieldNoSettingsFormatter.
|
||||
*/
|
||||
namespace Drupal\field_test\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'field_no_settings' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "field_no_settings",
|
||||
* label = @Translation("Field no settings"),
|
||||
* field_types = {
|
||||
* "test_field",
|
||||
* },
|
||||
* weight = -10
|
||||
* )
|
||||
*/
|
||||
class TestFieldNoSettingsFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items) {
|
||||
$elements = array();
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
// This formatter only needs to output raw for testing.
|
||||
$elements[$delta] = array('#markup' => $item->value);
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldFormatter\TestFieldPrepareViewFormatter.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'field_test_with_prepare_view' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "field_test_with_prepare_view",
|
||||
* label = @Translation("With prepare step"),
|
||||
* description = @Translation("Tests prepareView() method"),
|
||||
* field_types = {
|
||||
* "test_field"
|
||||
* },
|
||||
* weight = 10
|
||||
* )
|
||||
*/
|
||||
class TestFieldPrepareViewFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'test_formatter_setting_additional' => 'dummy test string',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element['test_formatter_setting_additional'] = array(
|
||||
'#title' => t('Setting'),
|
||||
'#type' => 'textfield',
|
||||
'#size' => 20,
|
||||
'#default_value' => $this->getSetting('test_formatter_setting_additional'),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
$summary[] = t('@setting: @value', array('@setting' => 'test_formatter_setting_additional', '@value' => $this->getSetting('test_formatter_setting_additional')));
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function prepareView(array $entities_items) {
|
||||
foreach ($entities_items as $items) {
|
||||
foreach ($items as $item) {
|
||||
// Don't add anything on empty values.
|
||||
if (!$item->isEmpty()) {
|
||||
$item->additional_formatter_value = $item->value + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items) {
|
||||
$elements = array();
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$elements[$delta] = array('#markup' => $this->getSetting('test_formatter_setting_additional') . '|' . $item->value . '|' . $item->additional_formatter_value);
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldType\HiddenTestItem.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\TypedData\DataDefinition;
|
||||
|
||||
/**
|
||||
* Defines the 'hidden_test' entity field item.
|
||||
*
|
||||
* @FieldType(
|
||||
* id = "hidden_test_field",
|
||||
* label = @Translation("Hidden from UI test field"),
|
||||
* description = @Translation("Dummy hidden field type used for tests."),
|
||||
* no_ui = TRUE,
|
||||
* default_widget = "test_field_widget",
|
||||
* default_formatter = "field_test_default"
|
||||
* )
|
||||
*/
|
||||
class HiddenTestItem extends TestItem {
|
||||
|
||||
}
|
|
@ -0,0 +1,176 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldType\TestItem.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\TypedData\DataDefinition;
|
||||
use Drupal\Core\Field\FieldItemBase;
|
||||
|
||||
/**
|
||||
* Defines the 'test_field' entity field item.
|
||||
*
|
||||
* @FieldType(
|
||||
* id = "test_field",
|
||||
* label = @Translation("Test field"),
|
||||
* description = @Translation("Dummy field type used for tests."),
|
||||
* default_widget = "test_field_widget",
|
||||
* default_formatter = "field_test_default"
|
||||
* )
|
||||
*/
|
||||
class TestItem extends FieldItemBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultStorageSettings() {
|
||||
return array(
|
||||
'test_field_storage_setting' => 'dummy test string',
|
||||
'changeable' => 'a changeable field storage setting',
|
||||
'unchangeable' => 'an unchangeable field storage setting',
|
||||
) + parent::defaultStorageSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultFieldSettings() {
|
||||
return array(
|
||||
'test_field_setting' => 'dummy test string',
|
||||
) + parent::defaultFieldSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
|
||||
$properties['value'] = DataDefinition::create('integer')
|
||||
->setLabel(t('Test integer value'))
|
||||
->setRequired(TRUE);
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function schema(FieldStorageDefinitionInterface $field_definition) {
|
||||
return array(
|
||||
'columns' => array(
|
||||
'value' => array(
|
||||
'type' => 'int',
|
||||
'size' => 'medium',
|
||||
),
|
||||
),
|
||||
'indexes' => array(
|
||||
'value' => array('value'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
|
||||
$form['test_field_storage_setting'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Field test field storage setting'),
|
||||
'#default_value' => $this->getSetting('test_field_storage_setting'),
|
||||
'#required' => FALSE,
|
||||
'#description' => t('A dummy form element to simulate field storage setting.'),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
|
||||
$form['test_field_setting'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Field test field setting'),
|
||||
'#default_value' => $this->getSetting('test_field_setting'),
|
||||
'#required' => FALSE,
|
||||
'#description' => t('A dummy form element to simulate field setting.'),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete() {
|
||||
// Reports that delete() method is executed for testing purposes.
|
||||
field_test_memorize('field_test_field_delete', array($this->getEntity()));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConstraints() {
|
||||
$constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
|
||||
$constraints = parent::getConstraints();
|
||||
|
||||
$constraints[] = $constraint_manager->create('ComplexData', array(
|
||||
'value' => array(
|
||||
'TestField' => array(
|
||||
'value' => -1,
|
||||
'message' => t('%name does not accept the value @value.', array('%name' => $this->getFieldDefinition()->getLabel(), '@value' => -1)),
|
||||
)
|
||||
),
|
||||
));
|
||||
|
||||
return $constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isEmpty() {
|
||||
return empty($this->value);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function storageSettingsToConfigData(array $settings) {
|
||||
$settings['config_data_from_storage_setting'] = 'TRUE';
|
||||
unset($settings['storage_setting_from_config_data']);
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function storageSettingsFromConfigData(array $settings) {
|
||||
$settings['storage_setting_from_config_data'] = 'TRUE';
|
||||
unset($settings['config_data_from_storage_setting']);
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function fieldSettingsToConfigData(array $settings) {
|
||||
$settings['config_data_from_field_setting'] = 'TRUE';
|
||||
unset($settings['field_setting_from_config_data']);
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function fieldSettingsFromConfigData(array $settings) {
|
||||
$settings['field_setting_from_config_data'] = 'TRUE';
|
||||
unset($settings['config_data_from_field_setting']);
|
||||
return $settings;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldType\TestItemWithDependencies.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Defines the 'test_field_with_dependencies' entity field item.
|
||||
*
|
||||
* @FieldType(
|
||||
* id = "test_field_with_dependencies",
|
||||
* label = @Translation("Test field with dependencies"),
|
||||
* description = @Translation("Dummy field type used for tests."),
|
||||
* default_widget = "test_field_widget",
|
||||
* default_formatter = "field_test_default",
|
||||
* config_dependencies = {
|
||||
* "module" = {
|
||||
* "test_module"
|
||||
* }
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
|
||||
class TestItemWithDependencies extends TestItem {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
|
||||
return ['content' => ['node:article:uuid']];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldType\TestItemWithPreconfiguredOptions.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface;
|
||||
|
||||
/**
|
||||
* Defines the 'test_field_with_preconfigured_options' entity field item.
|
||||
*
|
||||
* @FieldType(
|
||||
* id = "test_field_with_preconfigured_options",
|
||||
* label = @Translation("Test field with preconfigured options"),
|
||||
* description = @Translation("Dummy field type used for tests."),
|
||||
* default_widget = "test_field_widget",
|
||||
* default_formatter = "field_test_default"
|
||||
* )
|
||||
*/
|
||||
class TestItemWithPreconfiguredOptions extends TestItem implements PreconfiguredFieldUiOptionsInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getPreconfiguredOptions() {
|
||||
return [
|
||||
'custom_options' => [
|
||||
'label' => t('All custom options'),
|
||||
'category' => t('Custom category'),
|
||||
'field_storage_config' => [
|
||||
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
|
||||
'settings' => [
|
||||
'test_field_storage_setting' => 'preconfigured_storage_setting'
|
||||
],
|
||||
],
|
||||
'field_config' => [
|
||||
'required' => TRUE,
|
||||
'settings' => [
|
||||
'test_field_setting' => 'preconfigured_field_setting',
|
||||
],
|
||||
],
|
||||
'entity_form_display' => [
|
||||
'type' => 'test_field_widget_multiple',
|
||||
],
|
||||
'entity_view_display' => [
|
||||
'type' => 'field_test_multiple',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldWidget\TestFieldWidget.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldWidget;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\WidgetBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Symfony\Component\Validator\ConstraintViolationInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'test_field_widget' widget.
|
||||
*
|
||||
* @FieldWidget(
|
||||
* id = "test_field_widget",
|
||||
* label = @Translation("Test widget"),
|
||||
* field_types = {
|
||||
* "test_field",
|
||||
* "hidden_test_field",
|
||||
* "test_field_with_preconfigured_options"
|
||||
* },
|
||||
* weight = -10
|
||||
* )
|
||||
*/
|
||||
class TestFieldWidget extends WidgetBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'test_widget_setting' => 'dummy test string',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element['test_widget_setting'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Field test field widget setting'),
|
||||
'#description' => t('A dummy form element to simulate field widget setting.'),
|
||||
'#default_value' => $this->getSetting('test_widget_setting'),
|
||||
'#required' => FALSE,
|
||||
);
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
$summary[] = t('@setting: @value', array('@setting' => 'test_widget_setting', '@value' => $this->getSetting('test_widget_setting')));
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
|
||||
$element += array(
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : '',
|
||||
);
|
||||
return array('value' => $element);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
|
||||
return $element['value'];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Field\FieldWidget\TestFieldWidgetMultiple.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Field\FieldWidget;
|
||||
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\WidgetBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Symfony\Component\Validator\ConstraintViolationInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'test_field_widget_multiple' widget.
|
||||
*
|
||||
* The 'field_types' entry is left empty, and is populated through
|
||||
* hook_field_widget_info_alter().
|
||||
*
|
||||
* @see field_test_field_widget_info_alter()
|
||||
*
|
||||
* @FieldWidget(
|
||||
* id = "test_field_widget_multiple",
|
||||
* label = @Translation("Test widget - multiple"),
|
||||
* multiple_values = TRUE,
|
||||
* weight = 10
|
||||
* )
|
||||
*/
|
||||
class TestFieldWidgetMultiple extends WidgetBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'test_widget_setting_multiple' => 'dummy test string',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element['test_widget_setting_multiple'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Field test field widget setting'),
|
||||
'#description' => t('A dummy form element to simulate field widget setting.'),
|
||||
'#default_value' => $this->getSetting('test_widget_setting_multiple'),
|
||||
'#required' => FALSE,
|
||||
);
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
$summary[] = t('@setting: @value', array('@setting' => 'test_widget_setting_multiple', '@value' => $this->getSetting('test_widget_setting_multiple')));
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
|
||||
$values = array();
|
||||
foreach ($items as $item) {
|
||||
$values[] = $item->value;
|
||||
}
|
||||
$element += array(
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => implode(', ', $values),
|
||||
'#element_validate' => array(array(get_class($this), 'multipleValidate')),
|
||||
);
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Element validation helper.
|
||||
*/
|
||||
public static function multipleValidate($element, FormStateInterface $form_state) {
|
||||
$values = array_map('trim', explode(',', $element['#value']));
|
||||
$items = array();
|
||||
foreach ($values as $value) {
|
||||
$items[] = array('value' => $value);
|
||||
}
|
||||
$form_state->setValueForElement($element, $items);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Used in \Drupal\entity_reference\Tests\EntityReferenceAdminTest::testAvailableFormatters().
|
||||
*/
|
||||
public static function isApplicable(FieldDefinitionInterface $field_definition) {
|
||||
// Returns FALSE if machine name of the field equals field_onewidgetfield.
|
||||
return $field_definition->getName() != "field_onewidgetfield";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\field_test\Plugin\Validation\Constraint\TestFieldConstraint.
|
||||
*/
|
||||
|
||||
namespace Drupal\field_test\Plugin\Validation\Constraint;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\NotEqualTo;
|
||||
|
||||
/**
|
||||
* Checks if a value is not equal.
|
||||
*
|
||||
* @Constraint(
|
||||
* id = "TestField",
|
||||
* label = @Translation("Test Field", context = "Validation"),
|
||||
* type = { "integer" }
|
||||
* )
|
||||
*/
|
||||
class TestFieldConstraint extends NotEqualTo {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRequiredOptions() {
|
||||
return array('value');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validatedBy() {
|
||||
return '\Symfony\Component\Validator\Constraints\NotEqualToValidator';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
id: entity_test.entity_test.field_test_import
|
||||
langcode: en
|
||||
field_name: field_test_import
|
||||
entity_type: entity_test
|
||||
bundle: entity_test
|
||||
label: 'Test import field'
|
||||
description: ''
|
||||
required: false
|
||||
default_value: { }
|
||||
default_value_callback: ''
|
||||
settings: { }
|
||||
field_type: text
|
||||
dependencies:
|
||||
config:
|
||||
- field.storage.entity_test.field_test_import
|
|
@ -0,0 +1,15 @@
|
|||
id: entity_test.entity_test.field_test_import_2
|
||||
langcode: en
|
||||
field_name: field_test_import_2
|
||||
entity_type: entity_test
|
||||
bundle: entity_test
|
||||
label: 'Test import field 2 on entity_test bundle'
|
||||
description: ''
|
||||
required: false
|
||||
default_value: { }
|
||||
default_value_callback: ''
|
||||
settings: { }
|
||||
field_type: text
|
||||
dependencies:
|
||||
config:
|
||||
- field.storage.entity_test.field_test_import_2
|
|
@ -0,0 +1,15 @@
|
|||
id: entity_test.test_bundle.field_test_import_2
|
||||
langcode: en
|
||||
field_name: field_test_import_2
|
||||
entity_type: entity_test
|
||||
bundle: test_bundle
|
||||
label: 'Test import field 2 on test bundle'
|
||||
description: ''
|
||||
required: false
|
||||
default_value: { }
|
||||
default_value_callback: ''
|
||||
settings: { }
|
||||
field_type: text
|
||||
dependencies:
|
||||
config:
|
||||
- field.storage.entity_test.field_test_import_2
|
|
@ -0,0 +1,19 @@
|
|||
id: entity_test.field_test_import
|
||||
langcode: en
|
||||
field_name: field_test_import
|
||||
entity_type: entity_test
|
||||
type: text
|
||||
settings:
|
||||
max_length: 255
|
||||
module: text
|
||||
locked: false
|
||||
cardinality: 1
|
||||
translatable: false
|
||||
indexes:
|
||||
format:
|
||||
- format
|
||||
dependencies:
|
||||
module:
|
||||
- entity_test
|
||||
- text
|
||||
persist_with_no_fields: false
|
|
@ -0,0 +1,19 @@
|
|||
id: entity_test.field_test_import_2
|
||||
langcode: en
|
||||
field_name: field_test_import_2
|
||||
entity_type: entity_test
|
||||
type: text
|
||||
settings:
|
||||
max_length: 255
|
||||
module: text
|
||||
locked: false
|
||||
cardinality: 1
|
||||
translatable: false
|
||||
indexes:
|
||||
format:
|
||||
- format
|
||||
dependencies:
|
||||
module:
|
||||
- entity_test
|
||||
- text
|
||||
persist_with_no_fields: false
|
|
@ -0,0 +1,6 @@
|
|||
name: 'Field API configuration tests'
|
||||
type: module
|
||||
description: 'Support module for the Field API configuration tests.'
|
||||
core: 8.x
|
||||
package: Testing
|
||||
version: VERSION
|
|
@ -0,0 +1,16 @@
|
|||
id: entity_test.entity_test.field_test_import_staging
|
||||
uuid: ea711065-6940-47cd-813d-618f64095481
|
||||
langcode: en
|
||||
field_name: field_test_import_staging
|
||||
entity_type: entity_test
|
||||
bundle: entity_test
|
||||
label: 'Import from staging'
|
||||
description: ''
|
||||
required: '0'
|
||||
default_value: { }
|
||||
default_value_callback: ''
|
||||
settings: { }
|
||||
field_type: text
|
||||
dependencies:
|
||||
config:
|
||||
- field.storage.entity_test.field_test_import_staging
|
|
@ -0,0 +1,16 @@
|
|||
id: entity_test.test_bundle.field_test_import_staging_2
|
||||
uuid: f07794a2-d7cc-45b6-b40d-13cf021b5552
|
||||
langcode: en
|
||||
field_name: field_test_import_staging_2
|
||||
entity_type: entity_test
|
||||
bundle: test_bundle
|
||||
label: 'Test import field 2 on test bundle'
|
||||
description: ''
|
||||
required: '0'
|
||||
default_value: { }
|
||||
default_value_callback: ''
|
||||
settings: { }
|
||||
field_type: text
|
||||
dependencies:
|
||||
config:
|
||||
- field.storage.entity_test.field_test_import_staging_2
|
|
@ -0,0 +1,16 @@
|
|||
id: entity_test.test_bundle_2.field_test_import_staging_2
|
||||
uuid: 49d6dd19-5097-443d-8f00-fc79525bebce
|
||||
langcode: en
|
||||
field_name: field_test_import_staging_2
|
||||
entity_type: entity_test
|
||||
bundle: test_bundle_2
|
||||
label: 'Test import field 2 on test bundle 2'
|
||||
description: ''
|
||||
required: '0'
|
||||
default_value: { }
|
||||
default_value_callback: ''
|
||||
settings: { }
|
||||
field_type: text
|
||||
dependencies:
|
||||
config:
|
||||
- field.storage.entity_test.field_test_import_staging_2
|
|
@ -0,0 +1,20 @@
|
|||
id: entity_test.field_test_import_staging
|
||||
uuid: 0bf654cc-f14a-4881-b94c-76959e47466b
|
||||
langcode: en
|
||||
field_name: field_test_import_staging
|
||||
entity_type: entity_test
|
||||
type: text
|
||||
settings:
|
||||
max_length: '255'
|
||||
module: text
|
||||
locked: '0'
|
||||
cardinality: '1'
|
||||
translatable: '0'
|
||||
indexes:
|
||||
format:
|
||||
- format
|
||||
dependencies:
|
||||
module:
|
||||
- entity_test
|
||||
- text
|
||||
persist_with_no_fields: false
|
|
@ -0,0 +1,20 @@
|
|||
id: entity_test.field_test_import_staging_2
|
||||
uuid: 2165d9aa-9a0c-41a1-be02-2a49f3405c00
|
||||
langcode: en
|
||||
field_name: field_test_import_staging_2
|
||||
entity_type: entity_test
|
||||
type: text
|
||||
settings:
|
||||
max_length: '255'
|
||||
module: text
|
||||
locked: '0'
|
||||
cardinality: '1'
|
||||
translatable: '0'
|
||||
indexes:
|
||||
format:
|
||||
- format
|
||||
dependencies:
|
||||
module:
|
||||
- entity_test
|
||||
- text
|
||||
persist_with_no_fields: false
|
|
@ -0,0 +1,8 @@
|
|||
name: 'User test views'
|
||||
type: module
|
||||
description: 'Provides default views for views user tests.'
|
||||
package: Testing
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
dependencies:
|
||||
- views
|
|
@ -0,0 +1,64 @@
|
|||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
module:
|
||||
- node
|
||||
- user
|
||||
id: test_view_fieldapi
|
||||
label: test_view_fieldapi
|
||||
module: views
|
||||
description: ''
|
||||
tag: default
|
||||
base_table: node
|
||||
base_field: nid
|
||||
core: '8'
|
||||
display:
|
||||
default:
|
||||
display_options:
|
||||
access:
|
||||
type: perm
|
||||
fields:
|
||||
nid:
|
||||
field: nid
|
||||
id: nid
|
||||
table: node
|
||||
plugin_id: field
|
||||
entity_type: node
|
||||
entity_field: nid
|
||||
field_name_0:
|
||||
id: field_name_0
|
||||
table: node__field_name_0
|
||||
field: field_name_0
|
||||
plugin_id: field
|
||||
entity_type: node
|
||||
entity_field: field_name_0
|
||||
field_name_5:
|
||||
id: field_name_5
|
||||
table: node__field_name_5
|
||||
field: field_name_5
|
||||
plugin_id: field
|
||||
entity_type: node
|
||||
entity_field: field_name_5
|
||||
field_no_view_access:
|
||||
id: field_no_view_access
|
||||
table: node__field_no_view_access
|
||||
field: field_no_view_access
|
||||
plugin_id: field
|
||||
entity_type: node
|
||||
entity_field: field_no_view_access
|
||||
cache:
|
||||
type: tag
|
||||
exposed_form:
|
||||
type: basic
|
||||
pager:
|
||||
type: full
|
||||
query:
|
||||
type: views_query
|
||||
style:
|
||||
type: default
|
||||
row:
|
||||
type: fields
|
||||
display_plugin: default
|
||||
display_title: Master
|
||||
id: default
|
||||
position: 0
|
|
@ -0,0 +1,15 @@
|
|||
field.formatter.third_party.field_third_party_test:
|
||||
type: mapping
|
||||
label: 'field_third_party_test third party formatter settings'
|
||||
mapping:
|
||||
field_test_field_formatter_third_party_settings_form:
|
||||
type: string
|
||||
label: field_test_field_formatter_third_party_settings_form
|
||||
|
||||
field.widget.third_party.field_third_party_test:
|
||||
type: mapping
|
||||
label: 'field_third_party_test third party widget settings'
|
||||
mapping:
|
||||
field_test_widget_third_party_settings_form:
|
||||
type: string
|
||||
label: field_test_widget_third_party_settings_form
|
|
@ -0,0 +1,9 @@
|
|||
name: 'Field Third Party Settings Test'
|
||||
type: module
|
||||
description: 'Support module for the Field API tests.'
|
||||
core: 8.x
|
||||
package: Testing
|
||||
version: VERSION
|
||||
dependencies:
|
||||
- entity_test
|
||||
- field_test
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_third_party_settings_form().
|
||||
*/
|
||||
function field_third_party_test_field_widget_third_party_settings_form(\Drupal\Core\Field\WidgetInterface $plugin, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
|
||||
$element['field_test_widget_third_party_settings_form'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('3rd party widget settings form'),
|
||||
'#default_value' => $plugin->getThirdPartySetting('field_third_party_test', 'field_test_widget_third_party_settings_form'),
|
||||
);
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_settings_summary_alter().
|
||||
*/
|
||||
function field_third_party_test_field_widget_settings_summary_alter(&$summary, $context) {
|
||||
$summary[] = 'field_test_field_widget_settings_summary_alter';
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_third_party_settings_form().
|
||||
*/
|
||||
function field_third_party_test_field_formatter_third_party_settings_form(\Drupal\Core\Field\FormatterInterface $plugin, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, $view_mode, $form, FormStateInterface $form_state) {
|
||||
$element['field_test_field_formatter_third_party_settings_form'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('3rd party formatter settings form'),
|
||||
'#default_value' => $plugin->getThirdPartySetting('field_third_party_test', 'field_test_field_formatter_third_party_settings_form'),
|
||||
);
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_settings_summary_alter().
|
||||
*/
|
||||
function field_third_party_test_field_formatter_settings_summary_alter(&$summary, $context) {
|
||||
$summary[] = 'field_test_field_formatter_settings_summary_alter';
|
||||
return $summary;
|
||||
}
|
||||
|
309
core/modules/field/tests/src/Unit/FieldConfigEntityUnitTest.php
Normal file
309
core/modules/field/tests/src/Unit/FieldConfigEntityUnitTest.php
Normal file
|
@ -0,0 +1,309 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\field\Unit;
|
||||
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\field\Entity\FieldConfig
|
||||
* @group field
|
||||
*/
|
||||
class FieldConfigEntityUnitTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The entity type used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $entityType;
|
||||
|
||||
/**
|
||||
* The entity manager used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* The ID of the type of the entity under test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $entityTypeId;
|
||||
|
||||
/**
|
||||
* The UUID generator used for testing.
|
||||
*
|
||||
* @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $uuid;
|
||||
|
||||
/**
|
||||
* The mock field storage.
|
||||
*
|
||||
* @var \Drupal\field\FieldStorageConfigInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $fieldStorage;
|
||||
|
||||
/**
|
||||
* The typed configuration manager used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $typedConfigManager;
|
||||
|
||||
/**
|
||||
* The mock field type plugin manager;
|
||||
*
|
||||
* @var \Drupal\Core\Field\FieldTypePluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $fieldTypePluginManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->entityTypeId = $this->randomMachineName();
|
||||
$this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
|
||||
|
||||
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
|
||||
|
||||
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
|
||||
|
||||
$this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
|
||||
|
||||
$this->fieldTypePluginManager = $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface');
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$container->set('entity.manager', $this->entityManager);
|
||||
$container->set('uuid', $this->uuid);
|
||||
$container->set('config.typed', $this->typedConfigManager);
|
||||
$container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
|
||||
\Drupal::setContainer($container);
|
||||
|
||||
// Create a mock FieldStorageConfig object.
|
||||
$this->fieldStorage = $this->getMock('\Drupal\field\FieldStorageConfigInterface');
|
||||
$this->fieldStorage->expects($this->any())
|
||||
->method('getType')
|
||||
->will($this->returnValue('test_field'));
|
||||
$this->fieldStorage->expects($this->any())
|
||||
->method('getName')
|
||||
->will($this->returnValue('field_test'));
|
||||
$this->fieldStorage->expects($this->any())
|
||||
->method('getSettings')
|
||||
->willReturn(array());
|
||||
// Place the field in the mocked entity manager's field registry.
|
||||
$this->entityManager->expects($this->any())
|
||||
->method('getFieldStorageDefinitions')
|
||||
->with('test_entity_type')
|
||||
->will($this->returnValue(array(
|
||||
$this->fieldStorage->getName() => $this->fieldStorage,
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::calculateDependencies
|
||||
*/
|
||||
public function testCalculateDependencies() {
|
||||
// Mock the interfaces necessary to create a dependency on a bundle entity.
|
||||
$bundle_entity = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
|
||||
$bundle_entity->expects($this->any())
|
||||
->method('getConfigDependencyName')
|
||||
->will($this->returnValue('test.test_entity_type.id'));
|
||||
|
||||
$storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
|
||||
$storage->expects($this->any())
|
||||
->method('load')
|
||||
->with('test_bundle')
|
||||
->will($this->returnValue($bundle_entity));
|
||||
|
||||
$this->entityManager->expects($this->any())
|
||||
->method('getStorage')
|
||||
->with('bundle_entity_type')
|
||||
->will($this->returnValue($storage));
|
||||
|
||||
$target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
||||
$target_entity_type->expects($this->any())
|
||||
->method('getBundleEntityType')
|
||||
->will($this->returnValue('bundle_entity_type'));
|
||||
|
||||
$this->entityManager->expects($this->at(0))
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->willReturn($this->entityType);
|
||||
$this->entityManager->expects($this->at(1))
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->willReturn($this->entityType);
|
||||
$this->entityManager->expects($this->at(2))
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->willReturn($this->entityType);
|
||||
$this->entityManager->expects($this->at(3))
|
||||
->method('getDefinition')
|
||||
->with('test_entity_type')
|
||||
->willReturn($target_entity_type);
|
||||
|
||||
$this->fieldTypePluginManager->expects($this->any())
|
||||
->method('getDefinition')
|
||||
->with('test_field')
|
||||
->willReturn(['provider' => 'test_module', 'config_dependencies' =>['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
|
||||
|
||||
$this->fieldStorage->expects($this->once())
|
||||
->method('getConfigDependencyName')
|
||||
->will($this->returnValue('field.storage.test_entity_type.test_field'));
|
||||
|
||||
$field = new FieldConfig(array(
|
||||
'field_name' => $this->fieldStorage->getName(),
|
||||
'entity_type' => 'test_entity_type',
|
||||
'bundle' => 'test_bundle',
|
||||
'field_type' => 'test_field',
|
||||
), $this->entityTypeId);
|
||||
$dependencies = $field->calculateDependencies();
|
||||
$this->assertContains('field.storage.test_entity_type.test_field', $dependencies['config']);
|
||||
$this->assertContains('test.test_entity_type.id', $dependencies['config']);
|
||||
$this->assertEquals(['test_module', 'test_module2', 'test_module3'], $dependencies['module']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that invalid bundles are handled.
|
||||
*
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage Missing bundle entity, entity type <em class="placeholder">bundle_entity_type</em>, entity id <em class="placeholder">test_bundle_not_exists</em>.
|
||||
*/
|
||||
public function testCalculateDependenciesIncorrectBundle() {
|
||||
$storage = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
|
||||
$storage->expects($this->any())
|
||||
->method('load')
|
||||
->with('test_bundle_not_exists')
|
||||
->will($this->returnValue(NULL));
|
||||
|
||||
$this->entityManager->expects($this->any())
|
||||
->method('getStorage')
|
||||
->with('bundle_entity_type')
|
||||
->will($this->returnValue($storage));
|
||||
|
||||
$target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
||||
$target_entity_type->expects($this->any())
|
||||
->method('getBundleEntityType')
|
||||
->will($this->returnValue('bundle_entity_type'));
|
||||
|
||||
$this->entityManager->expects($this->at(0))
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->willReturn($this->entityType);
|
||||
$this->entityManager->expects($this->at(1))
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->willReturn($this->entityType);
|
||||
$this->entityManager->expects($this->at(2))
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->willReturn($this->entityType);
|
||||
$this->entityManager->expects($this->at(3))
|
||||
->method('getDefinition')
|
||||
->with('test_entity_type')
|
||||
->willReturn($target_entity_type);
|
||||
|
||||
$this->fieldTypePluginManager->expects($this->any())
|
||||
->method('getDefinition')
|
||||
->with('test_field')
|
||||
->willReturn(['provider' => 'test_module', 'config_dependencies' =>['module' => ['test_module2']], 'class' => '\Drupal\Tests\field\Unit\DependencyFieldItem']);
|
||||
|
||||
$field = new FieldConfig(array(
|
||||
'field_name' => $this->fieldStorage->getName(),
|
||||
'entity_type' => 'test_entity_type',
|
||||
'bundle' => 'test_bundle_not_exists',
|
||||
'field_type' => 'test_field',
|
||||
), $this->entityTypeId);
|
||||
$field->calculateDependencies();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::toArray
|
||||
*/
|
||||
public function testToArray() {
|
||||
$field = new FieldConfig(array(
|
||||
'field_name' => $this->fieldStorage->getName(),
|
||||
'entity_type' => 'test_entity_type',
|
||||
'bundle' => 'test_bundle',
|
||||
'field_type' => 'test_field',
|
||||
), $this->entityTypeId);
|
||||
|
||||
$expected = array(
|
||||
'id' => 'test_entity_type.test_bundle.field_test',
|
||||
'uuid' => NULL,
|
||||
'status' => TRUE,
|
||||
'langcode' => 'en',
|
||||
'field_name' => 'field_test',
|
||||
'entity_type' => 'test_entity_type',
|
||||
'bundle' => 'test_bundle',
|
||||
'label' => '',
|
||||
'description' => '',
|
||||
'required' => FALSE,
|
||||
'default_value' => array(),
|
||||
'default_value_callback' => '',
|
||||
'settings' => array(),
|
||||
'dependencies' => array(),
|
||||
'field_type' => 'test_field',
|
||||
);
|
||||
$this->entityManager->expects($this->any())
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->will($this->returnValue($this->entityType));
|
||||
$this->entityType->expects($this->once())
|
||||
->method('getKey')
|
||||
->with('id')
|
||||
->will($this->returnValue('id'));
|
||||
$this->typedConfigManager->expects($this->once())
|
||||
->method('getDefinition')
|
||||
->will($this->returnValue(array('mapping' => array_fill_keys(array_keys($expected), ''))));
|
||||
|
||||
$export = $field->toArray();
|
||||
$this->assertEquals($expected, $export);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getType
|
||||
*/
|
||||
public function testGetType() {
|
||||
// Ensure that FieldConfig::getType() is not delegated to
|
||||
// FieldStorage.
|
||||
$this->entityManager->expects($this->never())
|
||||
->method('getFieldStorageDefinitions');
|
||||
$this->fieldStorage->expects($this->never())
|
||||
->method('getType');
|
||||
|
||||
$field = new FieldConfig(array(
|
||||
'field_name' => $this->fieldStorage->getName(),
|
||||
'entity_type' => 'test_entity_type',
|
||||
'bundle' => 'test_bundle',
|
||||
'field_type' => 'test_field',
|
||||
), $this->entityTypeId);
|
||||
|
||||
$this->assertEquals('test_field', $field->getType());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A test class.
|
||||
*
|
||||
* @see \Drupal\Tests\field\Unit\FieldConfigEntityUnitTest::testCalculateDependencies()
|
||||
*/
|
||||
class DependencyFieldItem {
|
||||
|
||||
public static function calculateDependencies(FieldDefinitionInterface $definition) {
|
||||
return ['module' => ['test_module3']];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\field\Unit\FieldStorageConfigEntityUnitTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\field\Unit;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\field\Entity\FieldStorageConfig
|
||||
*
|
||||
* @group field
|
||||
*/
|
||||
class FieldStorageConfigEntityUnitTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The entity manager used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* The ID of the type of the entity under test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $entityTypeId;
|
||||
|
||||
/**
|
||||
* The UUID generator used for testing.
|
||||
*
|
||||
* @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $uuid;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
|
||||
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$container->set('entity.manager', $this->entityManager);
|
||||
$container->set('uuid', $this->uuid);
|
||||
\Drupal::setContainer($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::calculateDependencies
|
||||
*/
|
||||
public function testCalculateDependencies() {
|
||||
// Create a mock entity type for FieldStorageConfig.
|
||||
$fieldStorageConfigentityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
|
||||
$fieldStorageConfigentityType->expects($this->any())
|
||||
->method('getProvider')
|
||||
->will($this->returnValue('field'));
|
||||
|
||||
// Create a mock entity type to attach the field to.
|
||||
$attached_entity_type_id = $this->randomMachineName();
|
||||
$attached_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
||||
$attached_entity_type->expects($this->any())
|
||||
->method('getProvider')
|
||||
->will($this->returnValue('entity_provider_module'));
|
||||
|
||||
// Get definition is called three times. Twice in
|
||||
// ConfigEntityBase::addDependency() to get the provider of the field config
|
||||
// entity type and once in FieldStorageConfig::calculateDependencies() to
|
||||
// get the provider of the entity type that field is attached to.
|
||||
$this->entityManager->expects($this->at(0))
|
||||
->method('getDefinition')
|
||||
->with('field_storage_config')
|
||||
->will($this->returnValue($fieldStorageConfigentityType));
|
||||
$this->entityManager->expects($this->at(1))
|
||||
->method('getDefinition')
|
||||
->with($attached_entity_type_id)
|
||||
->will($this->returnValue($attached_entity_type));
|
||||
$this->entityManager->expects($this->at(2))
|
||||
->method('getDefinition')
|
||||
->with('field_storage_config')
|
||||
->will($this->returnValue($fieldStorageConfigentityType));
|
||||
|
||||
$field_storage = new FieldStorageConfig(array(
|
||||
'entity_type' => $attached_entity_type_id,
|
||||
'field_name' => 'test_field',
|
||||
'type' => 'test_field_type',
|
||||
'module' => 'test_module',
|
||||
));
|
||||
|
||||
$dependencies = $field_storage->calculateDependencies();
|
||||
$this->assertContains('test_module', $dependencies['module']);
|
||||
$this->assertContains('entity_provider_module', $dependencies['module']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\field\Unit\FieldUninstallValidatorTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\field\Unit;
|
||||
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\field\FieldUninstallValidator
|
||||
* @group field
|
||||
*/
|
||||
class FieldUninstallValidatorTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @var \Drupal\field\FieldUninstallValidator|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $fieldUninstallValidator;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->fieldUninstallValidator = $this->getMockBuilder('Drupal\field\FieldUninstallValidator')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(['getFieldStoragesByModule'])
|
||||
->getMock();
|
||||
$this->fieldUninstallValidator->setStringTranslation($this->getStringTranslationStub());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::validate
|
||||
*/
|
||||
public function testValidateNoStorages() {
|
||||
$this->fieldUninstallValidator->expects($this->once())
|
||||
->method('getFieldStoragesByModule')
|
||||
->willReturn([]);
|
||||
|
||||
$module = $this->randomMachineName();
|
||||
$expected = [];
|
||||
$reasons = $this->fieldUninstallValidator->validate($module);
|
||||
$this->assertSame($expected, $reasons);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::validate
|
||||
*/
|
||||
public function testValidateDeleted() {
|
||||
$field_storage = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$field_storage->expects($this->once())
|
||||
->method('isDeleted')
|
||||
->willReturn(TRUE);
|
||||
$this->fieldUninstallValidator->expects($this->once())
|
||||
->method('getFieldStoragesByModule')
|
||||
->willReturn([$field_storage]);
|
||||
|
||||
$module = $this->randomMachineName();
|
||||
$expected = ['Fields pending deletion'];
|
||||
$reasons = $this->fieldUninstallValidator->validate($module);
|
||||
$this->assertSame($expected, $reasons);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::validate
|
||||
*/
|
||||
public function testValidateNoDeleted() {
|
||||
$field_storage = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$field_storage->expects($this->once())
|
||||
->method('isDeleted')
|
||||
->willReturn(FALSE);
|
||||
$this->fieldUninstallValidator->expects($this->once())
|
||||
->method('getFieldStoragesByModule')
|
||||
->willReturn([$field_storage]);
|
||||
|
||||
$module = $this->randomMachineName();
|
||||
$expected = ['Fields type(s) in use'];
|
||||
$reasons = $this->fieldUninstallValidator->validate($module);
|
||||
$this->assertSame($expected, $reasons);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue