/** * Tests the clearCachedDefinitions() method. * * @covers ::clearCachedDefinitions */ public function testClearCachedDefinitions() { $this->entityTypeManager->clearCachedDefinitions()->shouldBeCalled(); $this->entityTypeRepository->clearCachedDefinitions()->shouldBeCalled(); $this->entityTypeBundleInfo->clearCachedBundles()->shouldBeCalled(); $this->entityFieldManager->clearCachedFieldDefinitions()->shouldBeCalled(); $this->entityManager->clearCachedDefinitions(); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->keyValueFactory = $this->prophesize(KeyValueFactoryInterface::class); $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class); $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class); $this->cacheBackend = $this->prophesize(CacheBackendInterface::class); $this->fieldDefinitionListener = new FieldDefinitionListener($this->entityTypeManager->reveal(), $this->entityFieldManager->reveal(), $this->keyValueFactory->reveal(), $this->cacheBackend->reveal()); }
/** * {@inheritdoc} */ protected function alterViewTemplateAfterCreation(array &$view_template, $options = NULL) { parent::alterViewTemplateAfterCreation($view_template, $options); $field_defs = $this->field_manager->getBaseFieldDefinitions($this->getDefinitionValue('entity_type')); if (empty($field_defs['status'])) { // If entity doesn't have a base field status remove it from View filter. unset($view_template['display']['default']['display_options']['filters']['status']); } $this->field_manager->getFieldDefinitions($this->getDefinitionValue('entity_type'), 'event'); $this->field_manager->getFieldStorageDefinitions('node'); }
/** * Determine if this field is Entity Reference field that references a taxonomy term. * * @todo Change to a more generic is Content Entity Reference. * * @param $field_info * @param \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */ protected function isTermReferenceField($field_info, EntityFieldManagerInterface $field_manager) { if (!empty($field_info['type']) && $field_info['type'] == 'entity_reference_label') { if ($entity_type = $this->getTableEntityType($field_info['table'])) { $field_definitions = $field_manager->getFieldStorageDefinitions($entity_type); $field_definition = $field_definitions[$field_info['field']]; $target_type = $field_definition->getSetting('target_type'); return $target_type == 'taxonomy_term'; } } return FALSE; }
/** * {@inheritdoc} */ public function onBundleDelete($bundle, $entity_type_id) { $this->entityTypeBundleInfo->clearCachedBundles(); // Notify the entity storage. $storage = $this->entityTypeManager->getStorage($entity_type_id); if ($storage instanceof EntityBundleListenerInterface) { $storage->onBundleDelete($bundle, $entity_type_id); } // Invoke hook_entity_bundle_delete() hook. $this->moduleHandler->invokeAll('entity_bundle_delete', [$entity_type_id, $bundle]); $this->entityFieldManager->clearCachedFieldDefinitions(); }
/** * {@inheritdoc} */ public function getAllFields() { $map = $this->entityFieldManager->getFieldMap(); // Build a list of disqus comment fields only. $disqus_comment_fields = []; foreach ($map as $entity_type => $data) { foreach ($data as $field_name => $field_info) { if ($field_info['type'] == 'disqus_comment') { $disqus_comment_fields[$entity_type][$field_name] = $field_info; } } } return $disqus_comment_fields; }
/** * {@inheritdoc} */ public function onEntityTypeCreate(EntityTypeInterface $entity_type) { $entity_type_id = $entity_type->id(); // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. $storage = $this->entityTypeManager->getStorage($entity_type_id); if ($storage instanceof EntityTypeListenerInterface) { $storage->onEntityTypeCreate($entity_type); } $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type)); $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type); if ($entity_type->isSubclassOf(FieldableEntityInterface::class)) { $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id)); } }
/** * {@inheritdoc} */ public function tableFields($bundles) { $info = $this->entityTypeManager->getDefinition($this->entityTypeId()); $definitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entityTypeId()); $label_key = $info->getKey('label'); $label_field_label = t('Label'); if ($label_key && isset($definitions[$label_key])) { $label_field_label = $definitions[$label_key]->getLabel(); } $bundle_key = $info->getKey('bundle'); $bundle_field_label = t('Type'); if ($bundle_key && isset($definitions[$bundle_key])) { $bundle_field_label = $definitions[$bundle_key]->getLabel(); } $fields = []; $fields['label'] = [ 'type' => 'label', 'label' => $label_field_label, 'weight' => 1, ]; if (count($bundles) > 1) { $fields[$bundle_key] = [ 'type' => 'field', 'label' => $bundle_field_label, 'weight' => 2, ]; } return $fields; }
/** * Set all derivatives for an entity type. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * @param array $base_plugin_definition */ protected function setConfigurableFieldsDerivatives(EntityTypeInterface $entity_type, array $base_plugin_definition) { /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage */ $field_storages = $this->field_manager->getFieldStorageDefinitions($entity_type->id()); foreach ($field_storages as $field_id => $field_storage) { if ($field_storage->getType() == 'datetime') { $entity_type_id = $entity_type->id(); // Find better way to get table name. $field_table = $entity_type_id . '__' . $field_id; $field_table_data = $this->viewsData->get($field_table); if (isset($field_table_data[$field_id])) { $derivative = []; $field_info = $field_table_data[$field_id]; $derivative['field_id'] = $field_id; $join_tables = array_keys($field_table_data['table']['join']); // @todo Will there ever be more than 1 tables here? $join_table = array_pop($join_tables); $join_table_data = $this->viewsData->get($join_table); $derivative = ['replacements' => ['field_id' => $field_id, 'entity_type' => $entity_type_id, 'entity_label' => $entity_type->getLabel(), 'field_label' => $field_info['title'], 'base_table' => $join_table, 'field_table' => $field_table, 'default_field_id' => $this->getTableDefaultField($join_table_data, $entity_type_id), 'base_field' => $this->getTableBaseField($join_table_data)], 'view_template_id' => 'calendar_config_field']; $this->setDerivative($derivative, $base_plugin_definition); //$this->setDerivative($field_info, $entity_type, $field_table_data, $base_plugin_definition); } } } }
/** * {@inheritdoc} */ public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition) { $entity_type_id = $field_definition->getTargetEntityTypeId(); $bundle = $field_definition->getTargetBundle(); $field_name = $field_definition->getName(); // Notify the storage about the field deletion. $this->entityTypeManager->getStorage($entity_type_id)->onFieldDefinitionDelete($field_definition); // Unset the bundle from the bundle field map key value collection. $bundle_field_map = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->get($entity_type_id); unset($bundle_field_map[$field_name]['bundles'][$bundle]); if (empty($bundle_field_map[$field_name]['bundles'])) { // If there are no bundles left, remove the field from the map. unset($bundle_field_map[$field_name]); } $this->keyValueFactory->get('entity.definitions.bundle_field_map')->set($entity_type_id, $bundle_field_map); // Delete the cache entry. $this->cacheBackend->delete('entity_field_map'); // If the field map is initialized, update it as well, so that calls to it // do not have to rebuild it again. if ($field_map = $this->entityFieldManager->getFieldMap()) { unset($field_map[$entity_type_id][$field_name]['bundles'][$bundle]); if (empty($field_map[$entity_type_id][$field_name]['bundles'])) { unset($field_map[$entity_type_id][$field_name]); } $this->entityFieldManager->setFieldMap($field_map); } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $config = $this->config('pathauto.settings'); $form_state->cleanValues(); $original_entity_types = $config->get('enabled_entity_types'); foreach ($form_state->getValues() as $key => $value) { if ($key == 'enabled_entity_types') { $enabled_entity_types = []; foreach ($value as $entity_type_id => $enabled) { $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); // Verify that the entity type is enabled and that it is not defined // or defined by us before adding it to the configuration, so that // we do not store an entity type that cannot be enabled or disabled. if ($enabled && (!isset($field_definitions['path']) || $field_definitions['path']->getProvider() === 'pathauto')) { $enabled_entity_types[] = $entity_type_id; } } $value = $enabled_entity_types; } $config->set($key, $value); } $config->save(); // Clear cached field definitions if the values are changed. if ($original_entity_types != $config->get('enabled_entity_types')) { $this->entityFieldManager->clearCachedFieldDefinitions(); $this->aliasTypeManager->clearCachedDefinitions(); } parent::submitForm($form, $form_state); }
/** * {@inheritdoc} */ public function getReferencingFieldIds() { if (!isset($this->field_ids)) { $this->field_ids = []; $entity_reference_fields = $this->fieldManager->getFieldMapByFieldType('entity_reference'); $update_entity_type = $this->updateEntityType(); if (empty($entity_reference_fields[$update_entity_type])) { return $this->field_ids; } $entity_reference_fields = $entity_reference_fields[$update_entity_type]; foreach ($entity_reference_fields as $field_id => $entity_reference_field) { foreach ($entity_reference_field['bundles'] as $bundle) { $field = $this->fieldManager->getFieldDefinitions($update_entity_type, $bundle)[$field_id]; if ($field instanceof FieldConfig) { if ($field->getSetting('target_type') == 'scheduled_update' && !empty($field->getSetting('handler_settings')['target_bundles']) && in_array($this->configuration['updater_type'], $field->getSetting('handler_settings')['target_bundles']) ) { $this->field_ids[] = $field_id; } } } } } return $this->field_ids; }
/** * Get existing entity reference field on target entity type that reference scheduled updates. * * @return array */ protected function existingReferenceFields() { $entity_type = $this->entity->getUpdateEntityType(); $fields = $this->entityFieldManager->getFieldMapByFieldType('entity_reference'); if (!isset($fields[$entity_type])) { return []; } $fields = $fields[$entity_type]; $ref_fields = []; foreach ($fields as $field_name => $field_info) { if ($definition = FieldStorageConfig::loadByName($entity_type, $field_name)) { $update_type = $definition->getSetting('target_type'); if ($update_type == 'scheduled_update') { $ref_fields[$field_name]['field_name'] = $field_name; $bundle_fields = []; foreach ($field_info['bundles'] as $bundle) { $field_config = FieldConfig::loadByName($entity_type, $bundle, $field_name); $bundle_fields[$bundle] = ['field_id' => $field_config->id(), 'label' => $field_config->label()]; } $ref_fields[$field_name]['bundles'] = $bundle_fields; } } } return $ref_fields; }
/** * Gets the field storage definition. * * @return \Drupal\Core\Field\FieldStorageDefinitionInterface */ protected function getFieldStorageDefinition() { if (empty($this->fieldStorageDefinition)) { $field_definitions = $this->entityFieldManager->getFieldStorageDefinitions($this->entityTypeId); $this->fieldStorageDefinition = $field_definitions[$this->fieldName]; } return $this->fieldStorageDefinition; }
/** * @covers ::getEntityTypeIdKeyType */ public function testGetEntityTypeIdKeyTypeNotFieldable() { $entity_type = $this->prophesize(EntityTypeInterface::class); $entity_type->isSubclassOf(FieldableEntityInterface::class)->willReturn(FALSE); $this->entityFieldManager->getFieldStorageDefinitions(Argument::any())->shouldNotBeCalled(); $type = $this->routeProvider->getEntityTypeIdKeyType($entity_type->reveal()); $this->assertNull($type); }
/** * Gets the type of the ID key for a given entity type. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * An entity type. * * @return string|null * The type of the ID key for a given entity type, or NULL if the entity * type does not support fields. */ protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type) { if (!$entity_type->isSubclassOf(FieldableEntityInterface::class)) { return NULL; } $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type->id()); return $field_storage_definitions[$entity_type->getKey('id')]->getType(); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $container = new ContainerBuilder(); // Register plugin managers used by Rules, but mock some unwanted // dependencies requiring more stuff to loaded. $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class); // Set all the modules as being existent. $this->enabledModules = new \ArrayObject(); $this->enabledModules['rules'] = TRUE; $this->enabledModules['rules_test'] = TRUE; $enabled_modules = $this->enabledModules; $this->moduleHandler->moduleExists(Argument::type('string'))->will(function ($arguments) use($enabled_modules) { return [$arguments[0], $enabled_modules[$arguments[0]]]; }); // Wed don't care about alter() calls on the module handler. $this->moduleHandler->alter(Argument::any(), Argument::any(), Argument::any(), Argument::any())->willReturn(NULL); $this->cacheBackend = new NullBackend('rules'); $rules_directory = __DIR__ . '/../../..'; $this->namespaces = new \ArrayObject(['Drupal\\rules' => $rules_directory . '/src', 'Drupal\\rules_test' => $rules_directory . '/tests/modules/rules_test/src', 'Drupal\\Core\\TypedData' => $this->root . '/core/lib/Drupal/Core/TypedData', 'Drupal\\Core\\Validation' => $this->root . '/core/lib/Drupal/Core/Validation']); $this->actionManager = new RulesActionManager($this->namespaces, $this->cacheBackend, $this->moduleHandler->reveal()); $this->conditionManager = new ConditionManager($this->namespaces, $this->cacheBackend, $this->moduleHandler->reveal()); $this->rulesExpressionManager = new ExpressionManager($this->namespaces, $this->moduleHandler->reveal()); $this->classResolver = $this->prophesize(ClassResolverInterface::class); $this->typedDataManager = new TypedDataManager($this->namespaces, $this->cacheBackend, $this->moduleHandler->reveal(), $this->classResolver->reveal()); $this->rulesDataProcessorManager = new DataProcessorManager($this->namespaces, $this->moduleHandler->reveal()); $this->aliasManager = $this->prophesize(AliasManagerInterface::class); // Keep the deprecated entity manager around because it is still used in a // few places. $this->entityManager = $this->prophesize(EntityManagerInterface::class); $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class); $this->entityTypeManager->getDefinitions()->willReturn([]); $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class); $this->entityFieldManager->getBaseFieldDefinitions()->willReturn([]); $this->entityTypeBundleInfo = $this->prophesize(EntityTypeBundleInfoInterface::class); $this->entityTypeBundleInfo->getBundleInfo()->willReturn([]); $this->dataFetcher = new DataFetcher(); $this->dataFilterManager = new DataFilterManager($this->namespaces, $this->moduleHandler->reveal()); $this->placeholderResolver = new PlaceholderResolver($this->dataFetcher, $this->dataFilterManager); $container->set('entity.manager', $this->entityManager->reveal()); $container->set('entity_type.manager', $this->entityTypeManager->reveal()); $container->set('entity_field.manager', $this->entityFieldManager->reveal()); $container->set('entity_type.bundle.info', $this->entityTypeBundleInfo->reveal()); $container->set('context.repository', new LazyContextRepository($container, [])); $container->set('path.alias_manager', $this->aliasManager->reveal()); $container->set('plugin.manager.rules_action', $this->actionManager); $container->set('plugin.manager.condition', $this->conditionManager); $container->set('plugin.manager.rules_expression', $this->rulesExpressionManager); $container->set('plugin.manager.rules_data_processor', $this->rulesDataProcessorManager); $container->set('typed_data_manager', $this->typedDataManager); $container->set('string_translation', $this->getStringTranslationStub()); $container->set('uuid', new Php()); $container->set('typed_data.data_fetcher', $this->dataFetcher); $container->set('typed_data.placeholder_resolver', $this->placeholderResolver); \Drupal::setContainer($container); $this->container = $container; }
/** * Helper function to get the field definitions. * * @param string $entityTypeId * The entity type we want to inspect. * @param string $bundle * The bundle we want to discover the fields of. * @return array * An array of field storage definitions for the entity type, * keyed by field name. */ private function getBundleFields($entityTypeId, $bundle) { $fields = []; if (!empty($entityTypeId) && !empty($bundle)) { $fields = array_filter($this->entityFieldManager->getFieldDefinitions($entityTypeId, $bundle), function ($fieldDefinition) { return $fieldDefinition instanceof FieldConfigInterface; }); } return $fields; }
public function getMainDiagram() { $entity_definitions = $this->entityTypeManager->getDefinitions(); $entities = []; $links = []; foreach ($entity_definitions as $definition) { $entity = ['id' => $definition->id(), 'type' => 'type', 'type_label' => t('Entity Type'), 'label' => $definition->getLabel(), 'provider' => $definition->getProvider(), 'group' => $definition->getGroup(), 'bundles' => []]; if ($definition instanceof ConfigEntityTypeInterface) { $entity['config_properties'] = $definition->getPropertiesToExport(); } $bundles = $this->entityTypeBundleInfo->getBundleInfo($definition->id()); foreach ($bundles as $bundle_id => $bundle_label) { $bundle = ['id' => $bundle_id, 'type' => 'bundle', 'type_label' => t('Entity Bundle'), 'label' => $bundle_label['label'], 'entity_type' => $definition->id()]; if ($definition->isSubclassOf(FieldableEntityInterface::class)) { $bundle['fields'] = []; $fields = $this->entityFieldManager->getFieldDefinitions($definition->id(), $bundle_id); foreach ($fields as $field) { $field_storage_definition = $field->getFieldStorageDefinition(); $field_settings = $field->getItemDefinition()->getSettings(); $field_name = $field_storage_definition->getName(); $bundle['fields'][$field_name] = ['id' => $field_name, 'label' => $field->getLabel(), 'type' => $field_storage_definition->getType(), 'description' => $field_storage_definition->getDescription(), 'cardinality' => $field_storage_definition->getCardinality(), 'is_multiple' => $field_storage_definition->isMultiple()]; $types[$field_storage_definition->getType()] = $field_storage_definition->getType(); if ($bundle['fields'][$field_name]['type'] == 'entity_reference') { $link = ['label' => t('Entity Reference from field "@field_name"', ['@field_name' => $field_name]), 'from' => 'bundle:' . $bundle_id, 'from_selector' => '.attribute-background-' . $field_name, 'targets' => ['type:' . $field_settings['target_type']]]; if (isset($field_settings['handler_settings']['target_bundles'])) { foreach ($field_settings['handler_settings']['target_bundles'] as $target_bundle) { $link['targets'][] = 'bundle:' . $target_bundle; } } $links[] = $link; } else { if ($bundle['fields'][$field_name]['type'] == 'image') { $links[] = ['label' => t('Image Reference from field "@field_name"', ['@field_name' => $field_name]), 'from' => 'bundle:' . $bundle_id, 'from_selector' => '.attribute-background-' . $field_name, 'targets' => ['type:' . $field_settings['target_type']]]; } } } } $entity['bundles'][$bundle_id] = $bundle; } $entities[$definition->id()] = $entity; } return ['#markup' => '<div class="erd-actions">' . '<i title="Add Entity Type or Bundle" class="erd-search">' . ' <input type="text"/>' . '</i>' . '<i title="Add editable label" class="erd-label"></i>' . '<i title="Change link styles" class="erd-line-style"></i>' . '<i title="Toggle machine names" class="erd-machine-name"></i>' . '<i title="Save to image" class="erd-save"></i>' . '<i title="Zoom in" class="erd-zoom"></i>' . '<i title="Zoom out" class="erd-unzoom"></i>' . '</div>' . '<div class="erd-container"></div>', '#allowed_tags' => ['input', 'div', 'i'], '#attached' => ['library' => ['erd/main'], 'drupalSettings' => ['erd' => ['entities' => $entities, 'links' => $links]]]]; }
/** * {@inheritdoc} */ public function getDerivativeDefinitions($base_plugin_definition) { foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { // An entity type must have a canonical link template and support fields. if ($entity_type->hasLinkTemplate('canonical') && is_subclass_of($entity_type->getClass(), FieldableEntityInterface::class)) { $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); if (!isset($base_fields['path'])) { // The entity type does not have a path field and is therefore not // supported. continue; } $this->derivatives[$entity_type_id] = $base_plugin_definition; $this->derivatives[$entity_type_id]['label'] = $entity_type->getLabel(); $this->derivatives[$entity_type_id]['types'] = [$this->tokenEntityMapper->getTokenTypeForEntityType($entity_type_id)]; $this->derivatives[$entity_type_id]['provider'] = $entity_type->getProvider(); $this->derivatives[$entity_type_id]['context'] = [$entity_type_id => new ContextDefinition("entity:{$entity_type_id}", $this->t('@label being aliased', ['@label' => $entity_type->getLabel()]))]; } } return $this->derivatives; }
/** * Gets a list of the file fields of the entity this field is attached to. * * @return array * An array of fields keyed by their id. */ public function getAvailableFields() { $available_fields = []; /** @var FieldDefinitionInterface $field_definition */ foreach ($this->entityFieldManager->getFieldDefinitions($this->targetEntityTypeId, $this->targetBundle) as $field_key => $field_definition) { if ($field_definition->getType() == 'file') { $available_fields[$field_key] = $field_definition->label(); } } return $available_fields; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); $values = $form_state->getValues(); $this->config('ds.settings')->set('field_template', $values['fs1']['field_template'])->set('ft-default', $values['fs1']['ft-default'])->set('ft-show-colon', $values['fs1']['ft-show-colon'])->save(); $this->entityFieldManager->clearCachedFieldDefinitions(); $this->moduleHandler->resetImplementations(); $this->themeRegistry->reset(); $this->routeBuilder->setRebuildNeeded(); \Drupal::cache('render')->deleteAll(); }
/** * {@inheritdoc} */ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) { $entity_type_id = $storage_definition->getTargetEntityTypeId(); // @todo Forward this to all interested handlers, not only storage, once // iterating handlers is possible: https://www.drupal.org/node/2332857. $storage = $this->entityTypeManager->getStorage($entity_type_id); if ($storage instanceof FieldStorageDefinitionListenerInterface) { $storage->onFieldStorageDefinitionDelete($storage_definition); } $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::DELETE, new FieldStorageDefinitionEvent($storage_definition)); $this->entityLastInstalledSchemaRepository->deleteLastInstalledFieldStorageDefinition($storage_definition); $this->entityFieldManager->clearCachedFieldDefinitions(); }
/** * {@inheritdoc} */ public function build(FacetInterface $facet, array $results) { $field_identifier = $facet->getFieldIdentifier(); $entity = 'node'; // Support multiple entities when using Search API. if ($facet->getFacetSource() instanceof SearchApiFacetSourceInterface) { $index = $facet->getFacetSource()->getIndex(); $field = $index->getField($field_identifier); $entity = str_replace('entity:', '', $field->getDatasourceId()); } // If it's an entity base field, we find it in the field definitions. // We don't have access to the bundle via SearchApiFacetSourceInterface, so // we check the entity's base fields only. $base_fields = $this->entityFieldManager->getFieldDefinitions($entity, ''); // This only works for configurable fields. $config_entity_name = sprintf('field.storage.%s.%s', $entity, $field_identifier); if (isset($base_fields[$field_identifier])) { $field = $base_fields[$field_identifier]; } elseif ($this->configManager->loadConfigEntityByName($config_entity_name) !== NULL) { $field = $this->configManager->loadConfigEntityByName($config_entity_name); } if ($field) { $function = $field->getSetting('allowed_values_function'); if (empty($function)) { $allowed_values = $field->getSetting('allowed_values'); } else { $allowed_values = ${$function}($field); } if (is_array($allowed_values)) { /** @var \Drupal\facets\Result\ResultInterface $result */ foreach ($results as &$result) { if (isset($allowed_values[$result->getRawValue()])) { $result->setDisplayValue($allowed_values[$result->getRawValue()]); } } } } return $results; }
/** * {@inheritdoc} */ public function getAllFieldConfigsForField(FieldStorageDefinitionInterface $definition, $entity_type_id) { $map = $this->entityFieldManager->getFieldMap()[$entity_type_id]; $definitions = []; $field_name = $definition->getName(); if (isset($map[$field_name])) { $bundles = $map[$field_name]['bundles']; foreach ($bundles as $bundle) { $definitions[$bundle] = $this->getFieldDefinition($entity_type_id, $bundle, $field_name); } } return $definitions; }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { /** @var \Drupal\contact\ContactFormInterface $contact_form */ $contact_form = $this->entity; // Get the original ID. $original_id = $contact_form->getOriginalId(); $new_id = $contact_form->id(); // Create the new form. $contact_form = $contact_form->createDuplicate(); $contact_form->set('id', $new_id); $contact_form->save(); // Clone configurable fields. foreach ($this->fieldManager->getFieldDefinitions('contact_message', $original_id) as $field) { if ($field instanceof BaseFieldDefinition) { continue; } if ($this->moduleHandler->moduleExists('field')) { if ($config = $field->getConfig($original_id)) { $new_config = FieldConfig::create([ 'bundle' => $contact_form->id(), 'uuid' => NULL, ] + $config->toArray()); $new_config->save(); } } } // Clone the entity form display. $display = EntityFormDisplay::load('contact_message.' . $original_id . '.default'); EntityFormDisplay::create([ 'bundle' => $contact_form->id(), 'uuid' => NULL, ] + $display->toArray())->save(); // Clone the entity view display. $display = EntityViewDisplay::load('contact_message.' . $original_id . '.default'); EntityViewDisplay::create([ 'bundle' => $contact_form->id(), 'uuid' => NULL, ] + $display->toArray())->save(); // Redirect and show messge. $form_state->setRedirect('entity.contact_form.edit_form', ['contact_form' => $contact_form->id()]); $edit_link = $this->entity->link($this->t('Edit')); drupal_set_message($this->t('Contact form %label has been added.', array('%label' => $contact_form->label()))); $this->logger('contact')->notice('Contact form %label has been added.', array('%label' => $contact_form->label(), 'link' => $edit_link)); }
/** * Gets the bundles for the current entity field. * * If the view has a non-exposed bundle filter, the bundles are taken from * there. Otherwise, the field's bundles are used. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The current entity type. * @param string $field_name * The current field name. * * @return string[] * The bundles. */ protected function getBundles(EntityTypeInterface $entity_type, $field_name) { $bundles = []; $bundle_key = $entity_type->getKey('bundle'); if ($bundle_key && isset($this->view->filter[$bundle_key])) { $filter = $this->view->filter[$bundle_key]; if (!$filter->isExposed() && !empty($filter->value)) { // 'all' is added by Views and isn't a bundle. $bundles = array_diff($filter->value, ['all']); } } // Fallback to the list of bundles the field is attached to. if (empty($bundles)) { $map = $this->entityFieldManager->getFieldMap(); $bundles = $map[$entity_type->id()][$field_name]['bundles']; } return $bundles; }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { $status = $this->entity->save(); // Update the default value of the status field. $product = $this->entityTypeManager->getStorage('commerce_product')->create(['type' => $this->entity->id()]); $value = (bool) $form_state->getValue('product_status'); if ($product->status->value != $value) { $fields = $this->entityFieldManager->getFieldDefinitions('commerce_product', $this->entity->id()); $fields['status']->getConfig($this->entity->id())->setDefaultValue($value)->save(); $this->entityFieldManager->clearCachedFieldDefinitions(); } drupal_set_message($this->t('The product type %label has been successfully saved.', ['%label' => $this->entity->label()])); $form_state->setRedirect('entity.commerce_product_type.collection'); if ($status == SAVED_NEW) { commerce_product_add_stores_field($this->entity); commerce_product_add_body_field($this->entity); commerce_product_add_variations_field($this->entity); } }
/** * {@inheritdoc} */ public function getTableFields($bundles) { $definitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType->id()); $label_key = $this->entityType->getKey('label'); $label_field_label = t('Label'); if ($label_key && isset($definitions[$label_key])) { $label_field_label = $definitions[$label_key]->getLabel(); } $bundle_key = $this->entityType->getKey('bundle'); $bundle_field_label = t('Type'); if ($bundle_key && isset($definitions[$bundle_key])) { $bundle_field_label = $definitions[$bundle_key]->getLabel(); } $fields = []; $fields['label'] = ['type' => 'label', 'label' => $label_field_label, 'weight' => 1]; if (count($bundles) > 1) { $fields[$bundle_key] = ['type' => 'field', 'label' => $bundle_field_label, 'weight' => 2, 'display_options' => ['type' => 'entity_reference_label', 'settings' => ['link' => FALSE]]]; } return $fields; }
/** * Utility to determine which view fields can be used for image data. * * This method will extract a list of fields that can be used as "sources" * for a Juicebox gallery along with other useful field information. * * @return array * An associative array containing a breakdown of field data that can be * referenced by other build methods, including: * - field_options_image: An associative array, keyed by field id, of fields * that can be used as Juicebox gallery image sources. * - field_options_image_type: An associative array, keyed by field id, of * field "types" for all fields listed in 'field_options_image' above. * - field_options: An associative array, keyed by field id, of fields that * cannot be used as Juicebox gallery image sources, but may be useful * for other purposes (text and caption sorces, etc.) */ public function confGetFieldSources() { $options = array('field_options_images' => array(), 'field_options_images_type' => array(), 'field_options' => array()); $view = $this->view; $field_handlers = $view->display_handler->getHandlers('field'); $field_labels = $view->display_handler->getFieldLabels(); // Separate image fields from non-image fields. For image fields we can // work with fids and fields of type image or file. foreach ($field_handlers as $field => $handler) { $is_image = FALSE; $id = $handler->getPluginId(); $name = $field_labels[$field]; if ($id == 'field') { // The field definition is on the handler, it's right bloody there, but // it's protected so we can't access it. This means we have to take the // long road (via our own injected entity manager) to get the field type // info. $entity_type = $handler->getEntityType(); $field_definition = $this->entityFieldManager->getFieldStorageDefinitions($entity_type)[$field]; $field_type = $field_definition->getType(); if ($field_type == 'image' || $field_type == 'file') { $field_cardinality = $field_definition->get('cardinality'); $options['field_options_images'][$field] = $name . ($field_cardinality == 1 ? '' : '*'); $options['field_options_images_type'][$field] = 'file_field'; $is_image = TRUE; } elseif ($field_type == 'integer' && $field == 'fid') { $options['field_options_images'][$field] = $name; $options['field_options_images_type'][$field] = 'file_id_field'; $is_image = TRUE; } } elseif ($id == 'file' && $field == 'fid') { $options['field_options_images'][$field] = $name; $options['field_options_images_type'][$field] = 'file_id_field'; $is_image = TRUE; } if (!$is_image) { $options['field_options'][$field] = $name; } } return $options; }