/** * {@inheritdoc} */ protected function mapToStorageRecord(EntityInterface $entity) { $record = parent::mapToStorageRecord($entity); $class = $this->fieldTypeManager->getPluginClass($record['field_type']); $record['settings'] = $class::fieldSettingsToConfigData($record['settings']); return $record; }
/** * Constructs a new DisplayOverviewBase. * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager * The field type manager. * @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager * The widget or formatter plugin manager. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The configuration factory. */ public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager, ConfigFactoryInterface $config_factory) { parent::__construct($entity_manager); $this->fieldTypes = $field_type_manager->getDefinitions(); $this->pluginManager = $plugin_manager; $this->configFactory = $config_factory; }
/** * Constructs a new FieldStorageConfigListBuilder object. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type definition. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager * The 'field type' plugin manager. */ public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, EntityTypeBundleInfoInterface $bundle_info_service) { parent::__construct($entity_type, $entity_manager->getStorage($entity_type->id())); $this->entityManager = $entity_manager; $this->bundles = $bundle_info_service->getAllBundleInfo(); $this->fieldTypeManager = $field_type_manager; $this->fieldTypes = $this->fieldTypeManager->getDefinitions(); }
/** * Constructs a new FieldStorageConfigListBuilder object. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type definition. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager * The 'field type' plugin manager. */ public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager) { parent::__construct($entity_type, $entity_manager->getStorage($entity_type->id())); $this->entityManager = $entity_manager; $this->bundles = entity_get_bundles(); $this->fieldTypeManager = $field_type_manager; $this->fieldTypes = $this->fieldTypeManager->getDefinitions(); }
/** * {@inheritdoc} */ public function getDerivativeDefinitions($base_plugin_definition) { foreach ($this->fieldTypePluginManager->getDefinitions() as $plugin_id => $definition) { $definition['definition_class'] = '\\Drupal\\Core\\Field\\TypedData\\FieldItemDataDefinition'; $definition['list_definition_class'] = '\\Drupal\\Core\\Field\\FieldDefinition'; $this->derivatives[$plugin_id] = $definition; } return $this->derivatives; }
/** * {@inheritdoc} */ public function calculateDependencies() { $this->dependencies = parent::calculateDependencies(); // Add a dependency on the module that provides the field type using the // source plugin configuration. $source_configuration = $this->migration->get('source'); if (isset($source_configuration['constants']['type'])) { $field_type = $this->fieldTypePluginManager->getDefinition($source_configuration['constants']['type']); $this->addDependency('module', $field_type['provider']); } return $this->dependencies; }
/** * Populates as much of the stub row as possible. * * @param \Drupal\migrate\Row $row * The row of data. */ protected function processStubRow(Row $row) { $bundle_key = $this->getKey('bundle'); if ($bundle_key && empty($row->getDestinationProperty($bundle_key))) { if (empty($this->bundles)) { throw new MigrateException('Stubbing failed, no bundles available for entity type: ' . $this->storage->getEntityTypeId()); } $row->setDestinationProperty($bundle_key, reset($this->bundles)); } // Populate any required fields not already populated. $fields = $this->entityManager->getFieldDefinitions($this->storage->getEntityTypeId(), $bundle_key); foreach ($fields as $field_name => $field_definition) { if ($field_definition->isRequired() && is_null($row->getDestinationProperty($field_name))) { // Use the configured default value for this specific field, if any. if ($default_value = $field_definition->getDefaultValueLiteral()) { $values[] = $default_value; } else { // Otherwise, ask the field type to generate a sample value. $field_type = $field_definition->getType(); /** @var \Drupal\Core\Field\FieldItemInterface $field_type_class */ $field_type_class = $this->fieldTypeManager->getPluginClass($field_definition->getType()); $values = $field_type_class::generateSampleValue($field_definition); if (is_null($values)) { // Handle failure to generate a sample value. throw new MigrateException('Stubbing failed, unable to generate value for field ' . $field_name); } } $row->setDestinationProperty($field_name, $values); } } }
/** * Returns an array of existing fields to be added to a bundle. * * @return array * An array of existing fields keyed by field name. */ protected function getExistingFieldOptions() { $options = array(); // Collect candidate field instances: all instances of fields for this // entity type that are not already present in the current bundle. $field_map = \Drupal::entityManager()->getFieldMap(); $instance_ids = array(); if (!empty($field_map[$this->entity_type])) { foreach ($field_map[$this->entity_type] as $field_name => $data) { if (!in_array($this->bundle, $data['bundles'])) { $bundle = reset($data['bundles']); $instance_ids[] = $this->entity_type . '.' . $bundle . '.' . $field_name; } } } // Load the instances and build the list of options. if ($instance_ids) { $field_types = $this->fieldTypeManager->getDefinitions(); $instances = $this->entityManager->getStorage('field_instance_config')->loadMultiple($instance_ids); foreach ($instances as $instance) { // Do not show: // - locked fields, // - fields that should not be added via user interface. $field_type = $instance->getType(); $field_storage = $instance->getFieldStorageDefinition(); if (empty($field_storage->locked) && empty($field_types[$field_type]['no_ui'])) { $options[$instance->getName()] = array('type' => $field_type, 'type_label' => $field_types[$field_type]['label'], 'field' => $instance->getName(), 'label' => $instance->getLabel()); } } } return $options; }
/** * @covers ::onDependencyRemoval */ public function testOnDependencyRemoval() { $this->fieldTypePluginManager->expects($this->any())->method('getDefinition')->with('test_field')->willReturn(['class' => '\\Drupal\\Tests\\field\\Unit\\DependencyFieldItem']); $field = new FieldConfig(['field_name' => $this->fieldStorage->getName(), 'entity_type' => 'test_entity_type', 'bundle' => 'test_bundle', 'field_type' => 'test_field', 'dependencies' => ['module' => ['fruiter']], 'third_party_settings' => ['fruiter' => ['fruit' => 'apple']]]); $changed = $field->onDependencyRemoval(['module' => ['fruiter']]); $this->assertTrue($changed); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->entityManager = $this->getMock('Drupal\\Core\\Entity\\EntityManagerInterface'); $this->formatterPluginManager = $this->getMockBuilder('Drupal\\Core\\Field\\FormatterPluginManager')->disableOriginalConstructor()->getMock(); $this->fieldTypePluginManager = $this->getMock('Drupal\\Core\\Field\\FieldTypePluginManagerInterface'); $this->fieldTypePluginManager->expects($this->any())->method('getDefaultStorageSettings')->willReturn([]); $this->fieldTypePluginManager->expects($this->any())->method('getDefaultFieldSettings')->willReturn([]); $this->languageManager = $this->getMock('Drupal\\Core\\Language\\LanguageManagerInterface'); $this->renderer = $this->getMock('Drupal\\Core\\Render\\RendererInterface'); $this->setupExecutableAndView(); $this->setupViewsData(); $this->display = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')->disableOriginalConstructor()->getMock(); $this->container = new ContainerBuilder(); $this->container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager); \Drupal::setContainer($this->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->any())->method('getDefinition')->willReturnMap([['field_storage_config', TRUE, $fieldStorageConfigentityType], [$attached_entity_type_id, TRUE, $attached_entity_type]]); $this->fieldTypeManager->expects($this->atLeastOnce())->method('getDefinition')->with('test_field_type', FALSE)->willReturn(['class' => TestFieldType::class]); $field_storage = new FieldStorageConfig(['entity_type' => $attached_entity_type_id, 'field_name' => 'test_field', 'type' => 'test_field_type', 'module' => 'test_module']); $dependencies = $field_storage->calculateDependencies()->getDependencies(); $this->assertEquals(['entity_provider_module', 'entity_test', 'test_module'], $dependencies['module']); $this->assertEquals(['stark'], $dependencies['theme']); }
/** * Test that invalid bundles are handled. * * @expectedException \LogicException * @expectedExceptionMessage Missing bundle entity, entity type bundle_entity_type, entity id test_bundle_not_exists. */ 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 = new EntityType(array('id' => 'test_entity_type', 'bundle_entity_type' => '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(); }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $field_config) { /** @var \Drupal\field\FieldConfigInterface $field_config */ $field_storage = $field_config->getFieldStorageDefinition(); $route_parameters = array('field_config' => $field_config->id()) + FieldUI::getRouteBundleParameter($this->entityManager->getDefinition($this->targetEntityTypeId), $this->targetBundle); $row = array('id' => Html::getClass($field_config->getName()), 'data' => array('label' => $field_config->getLabel(), 'field_name' => $field_config->getName(), 'field_type' => array('data' => array('#type' => 'link', '#title' => $this->fieldTypeManager->getDefinitions()[$field_storage->getType()]['label'], '#url' => Url::fromRoute("entity.field_config.{$this->targetEntityTypeId}_storage_edit_form", $route_parameters), '#options' => array('attributes' => array('title' => $this->t('Edit field settings.'))))))); // Add the operations. $row['data'] = $row['data'] + parent::buildRow($field_config); if ($field_storage->isLocked()) { $row['data']['operations'] = array('data' => array('#markup' => $this->t('Locked'))); $row['class'][] = 'menu-disabled'; } return $row; }
/** * Returns an array of existing field storages that can be added to a bundle. * * @return array * An array of existing field storages keyed by name. */ protected function getExistingFieldStorageOptions() { $options = array(); // Load the field_storages and build the list of options. $field_types = $this->fieldTypePluginManager->getDefinitions(); foreach ($this->entityManager->getFieldStorageDefinitions($this->entityTypeId) as $field_name => $field_storage) { // Do not show: // - non-configurable field storages, // - locked field storages, // - field storages that should not be added via user interface, // - field storages that already have a field in the bundle. $field_type = $field_storage->getType(); if ($field_storage instanceof FieldStorageConfigInterface && !$field_storage->isLocked() && empty($field_types[$field_type]['no_ui']) && !in_array($this->bundle, $field_storage->getBundles(), TRUE)) { $options[$field_name] = $this->t('@type: @field', array('@type' => $field_types[$field_type]['label'], '@field' => $field_name)); } } asort($options); return $options; }
/** * Returns an array of widget type options for a field type. * * @param string|null $field_type * (optional) The name of a field type, or NULL to retrieve all widget * options. Defaults to NULL. * * @return array * If no field type is provided, returns a nested array of all widget types, * keyed by field type human name. */ public function getOptions($field_type = NULL) { if (!isset($this->widgetOptions)) { $options = array(); $field_types = $this->fieldTypeManager->getDefinitions(); $widget_types = $this->getDefinitions(); uasort($widget_types, array('Drupal\\Component\\Utility\\SortArray', 'sortByWeightElement')); foreach ($widget_types as $name => $widget_type) { foreach ($widget_type['field_types'] as $widget_field_type) { // Check that the field type exists. if (isset($field_types[$widget_field_type])) { $options[$widget_field_type][$name] = $widget_type['label']; } } } $this->widgetOptions = $options; } if (isset($field_type)) { return !empty($this->widgetOptions[$field_type]) ? $this->widgetOptions[$field_type] : array(); } return $this->widgetOptions; }
/** * @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']); }
/** * Gets field type definition. * * @return array * The field type definition. */ protected function getFieldTypeDefinition() { return $this->fieldTypeManager->getDefinition($this->getFieldStorageDefinition()->getType()); }
/** * Constructs a new EntityDisplayFormBase. * * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager * The field type manager. * @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager * The widget or formatter plugin manager. */ public function __construct(FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager) { $this->fieldTypes = $field_type_manager->getDefinitions(); $this->pluginManager = $plugin_manager; }
/** * Returns the label for a specified field type. * * @param string $field_type * The field type. * * @return string * The field type label. */ protected function getFieldTypeLabel($field_type) { return $this->fieldTypeManager->getDefinitions()[$field_type]['label']; }
/** * Helper method to create an entity revision destination with mock services. * * @see \Drupal\Tests\migrate\Unit\Destination\EntityRevision * * @param $configuration * Configuration for the destination. * @param string $plugin_id * The plugin id. * @param array $plugin_definition * The plugin definition. * * @return \Drupal\Tests\migrate\Unit\destination\EntityRevision * Mocked destination. */ protected function getEntityRevisionDestination(array $configuration = [], $plugin_id = 'entity_revision', array $plugin_definition = []) { return new EntityRevision($configuration, $plugin_id, $plugin_definition, $this->migration->reveal(), $this->storage->reveal(), [], $this->entityManager->reveal(), $this->fieldTypeManager->reveal()); }
/** * {@inheritdoc} */ protected function defineOptions() { $options = parent::defineOptions(); $field_storage_definition = $this->getFieldStorageDefinition(); $field_type = $this->fieldTypePluginManager->getDefinition($field_storage_definition->getType()); $column_names = array_keys($field_storage_definition->getColumns()); $default_column = ''; // Try to determine a sensible default. if (count($column_names) == 1) { $default_column = $column_names[0]; } elseif (in_array('value', $column_names)) { $default_column = 'value'; } // If the field has a "value" column, we probably need that one. $options['click_sort_column'] = array('default' => $default_column); if (isset($this->definition['default_formatter'])) { $options['type'] = ['default' => $this->definition['default_formatter']]; } elseif (isset($field_type['default_formatter'])) { $options['type'] = ['default' => $field_type['default_formatter']]; } else { $options['type'] = ['default' => '']; } $options['settings'] = array('default' => isset($this->definition['default_formatter_settings']) ? $this->definition['default_formatter_settings'] : []); $options['group_column'] = array('default' => $default_column); $options['group_columns'] = array('default' => array()); // Options used for multiple value fields. $options['group_rows'] = array('default' => TRUE); // If we know the exact number of allowed values, then that can be // the default. Otherwise, default to 'all'. $options['delta_limit'] = array('default' => $field_storage_definition->getCardinality() > 1 ? $field_storage_definition->getCardinality() : 0); $options['delta_offset'] = array('default' => 0); $options['delta_reversed'] = array('default' => FALSE); $options['delta_first_last'] = array('default' => FALSE); $options['multi_type'] = array('default' => 'separator'); $options['separator'] = array('default' => ', '); $options['field_api_classes'] = array('default' => FALSE); return $options; }