/** * Get update runner for scheduled update types. * * @param array $update_types * Update ids to load runners for. If empty return all. * * @return \Drupal\scheduled_updates\Plugin\UpdateRunnerInterface[] */ protected function getUpdateTypeRunners(array $update_types = []) { $bundles = $this->entityTypeBundleInfo->getBundleInfo('scheduled_update'); /** @var UpdateRunnerInterface[] $runners */ $runners = []; foreach ($bundles as $bundle => $bundle_info) { if (empty($update_types) || in_array($bundle, $update_types)) { /** @var ScheduledUpdateType $updater */ $updater = ScheduledUpdateType::load($bundle); if ($runner = $this->getUpdateRunnerInstance($updater)) { $runners[$bundle] = $runner; } else { // @todo User logger for missing plugins /* drupal_set_message(t('Missing plugin in @plugin_id in update type @type', [ '@plugin_id' => $runner_settings['id'], '@type' => $updater->label(), ])); */ } } } return $runners; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $enabled_link = Link::fromTextAndUrl(t('enabled'), Url::fromRoute('system.modules_list')); $form['#attached']['library'][] = 'system/drupal.system'; $form['exclude_node_title_search'] = ['#type' => 'checkbox', '#title' => $this->t('Remove node title from search pages'), '#description' => $this->t('Select if you wish to remove title from search pages. You need to have Search module @link.', ['@link' => $enabled_link]), '#default_value' => $this->excludeNodeTitleManager->isSearchExcluded(), '#disabled' => !\Drupal::moduleHandler()->moduleExists('search')]; $form['content_type'] = ['#type' => 'fieldset', '#title' => $this->t('Exclude title by content types'), '#description' => $this->t('Define title excluding settings for each content type.'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE]; foreach ($this->bundleInfo->getBundleInfo('node') as $node_type => $node_type_info) { $form['#attached']['drupalSettings']['exclude_node_title']['content_types'][$node_type] = $node_type_info['label']; $form['content_type'][$node_type]['content_type_value'] = ['#type' => 'select', '#title' => $node_type_info['label'], '#default_value' => $this->excludeNodeTitleManager->getBundleExcludeMode($node_type), '#options' => ['none' => $this->t('None'), 'all' => $this->t('All nodes...'), 'user' => $this->t('User defined nodes...')]]; $entity_view_modes = $this->entityDisplayRepository->getViewModes('node'); $modes = []; foreach ($entity_view_modes as $view_mode_name => $view_mode_info) { $modes[$view_mode_name] = $view_mode_info['label']; } $modes += ['nodeform' => $this->t('Node form')]; switch ($form['content_type'][$node_type]['content_type_value']['#default_value']) { case 'all': $title = $this->t('Exclude title from all nodes in the following view modes:'); break; case 'user defined': $title = $this->t('Exclude title from user defined nodes in the following view modes:'); break; default: $title = $this->t('Exclude from:'); } $form['content_type'][$node_type]['content_type_modes'] = ['#type' => 'checkboxes', '#title' => $title, '#default_value' => $this->excludeNodeTitleManager->getExcludedViewModes($node_type), '#options' => $modes, '#states' => ['invisible' => ['select[name="content_type[' . $node_type . '][content_type_value]"]' => ['value' => 'none']]]]; } $form['#attached']['library'][] = 'exclude_node_title/drupal.exclude_node_title.admin'; return parent::buildForm($form, $form_state); }
/** * Checks access to create an entity of any bundle for the given route. * * @param \Symfony\Component\Routing\Route $route * The route to check against. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The parameterized route. * @param \Drupal\Core\Session\AccountInterface $account * The currently logged in account. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) { $entity_type_id = $route->getRequirement($this->requirementsKey); $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); $access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type_id); // In case there is no "bundle" entity key, check create access with no // bundle specified. if (!$entity_type->hasKey('bundle')) { return $access_control_handler->createAccess(NULL, $account, [], TRUE); } $access = AccessResult::neutral(); $bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id)); // Include list cache tag as access might change if more bundles are added. if ($entity_type->getBundleEntityType()) { $access->addCacheTags($this->entityTypeManager->getDefinition($entity_type->getBundleEntityType())->getListCacheTags()); // Check if the user is allowed to create new bundles. If so, allow // access, so the add page can show a link to create one. // @see \Drupal\Core\Entity\Controller\EntityController::addPage() $bundle_access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type->getBundleEntityType()); $access = $access->orIf($bundle_access_control_handler->createAccess(NULL, $account, [], TRUE)); if ($access->isAllowed()) { return $access; } } // Check whether an entity of any bundle may be created. foreach ($bundles as $bundle) { $access = $access->orIf($access_control_handler->createAccess($bundle, $account, [], TRUE)); // In case there is a least one bundle user can create entities for, // access is allowed. if ($access->isAllowed()) { break; } } return $access; }
/** * 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(); }
/** * 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(); }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $options = array(); $bundles = $this->entityTypeBundleInfo->getBundleInfo($this->bundleOf->id()); foreach ($bundles as $id => $info) { $options[$id] = $info['label']; } $form['bundles'] = array('#title' => $this->bundleOf->getBundleLabel(), '#type' => 'checkboxes', '#options' => $options, '#default_value' => $this->configuration['bundles']); return parent::buildConfigurationForm($form, $form_state); }
/** * {@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 getValueOptions() { if (!isset($this->valueOptions)) { $types = $this->bundleInfoService->getBundleInfo($this->entityTypeId); $this->valueTitle = $this->t('@entity types', array('@entity' => $this->entityType->getLabel())); $options = array(); foreach ($types as $type => $info) { $options[$type] = $info['label']; } asort($options); $this->valueOptions = $options; } return $this->valueOptions; }
/** * {@inheritdoc} */ public function getDerivativeDefinitions($base_plugin_definition) { // Also keep the 'entity' defined as is. $this->derivatives[''] = $base_plugin_definition; // Add definitions for each entity type and bundle. foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) { $this->derivatives[$entity_type_id] = array('label' => $entity_type->getLabel(), 'constraints' => $entity_type->getConstraints()) + $base_plugin_definition; // Incorporate the bundles as entity:$entity_type:$bundle, if any. foreach ($this->bundleInfoService->getBundleInfo($entity_type_id) as $bundle => $bundle_info) { if ($bundle !== $entity_type_id) { $this->derivatives[$entity_type_id . ':' . $bundle] = array('label' => $bundle_info['label'], 'constraints' => $this->derivatives[$entity_type_id]['constraints']) + $base_plugin_definition; } } } return $this->derivatives; }
/** * {@inheritdoc} */ public function preprocessTitle(&$vars, $node, $view_mode) { if ($this->isTitleExcluded($node, $view_mode)) { $node_info = $this->getNodeInfo($node); $node_type = $node_info['node_type']; switch ($view_mode) { case 'nodeform': $node_types = $this->bundleInfo->getBundleInfo('node'); if (!empty($vars['head_title'])) { $vars['head_title']['title'] = new TranslatableMarkup('Edit @nodetype', ['@nodetype' => $node_types[$node_type]['label']]); } elseif (!empty($vars['title'])) { $vars['title'] = new TranslatableMarkup('Edit @nodetype', ['@nodetype' => $node_types[$node_type]['label']]); } break; default: if (!empty($vars['title'])) { $vars['title'] = new HtmlEscapedText(''); } if (!empty($vars['page']) && is_array($vars['page'])) { $vars['page']['#title'] = new HtmlEscapedText(''); } if (!empty($vars['elements']) && is_array($vars['elements'])) { $vars['elements']['#title'] = new HtmlEscapedText(''); } if (!empty($vars['label']) && is_array($vars['elements'])) { $vars['label']['#title'] = new HtmlEscapedText(''); $vars['label']['#markup'] = new HtmlEscapedText(''); $vars['label'][0]['#context']['value'] = ''; } break; } } return $this; }
/** * {@inheritdoc} */ public function bundleOptions($entity_type) { $info = $this->bundleInfo->getBundleInfo($entity_type); $options = []; foreach ($info as $bundle => $bundle_info) { $options[$bundle] = $bundle_info['label']; } return $options; }
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]]]]; }
/** * Builds a list of entity type bundle options. * * Configuration entity types without a view builder are filtered out while * all other entity types are kept. * * @return array * An array of bundle labels, keyed by bundle name. */ protected function getEntityBundleOptions(EntityTypeInterface $entity_type) { $bundle_options = array(); // If the entity has bundles, allow option to restrict to bundle(s). if ($entity_type->hasKey('bundle')) { foreach ($this->entityTypeBundleInfo->getBundleInfo($entity_type->id()) as $bundle_id => $bundle_info) { $bundle_options[$bundle_id] = $bundle_info['label']; } natsort($bundle_options); } return $bundle_options; }
/** * Gets the target bundles for the current field. * * @return string[] * A list of bundles. */ protected function getTargetBundles() { $settings = $this->getFieldSettings(); if (!empty($settings['handler_settings']['target_bundles'])) { $target_bundles = array_values($settings['handler_settings']['target_bundles']); } else { // If no target bundles have been specified then all are available. $target_bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($settings['target_type'])); } return $target_bundles; }
/** * Provides a generic add title callback for entities with bundles. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * @param string $entity_type_id * The entity type ID. * @param string $bundle_parameter * The name of the route parameter that holds the bundle. * * @return string * The title for the entity add page, if the bundle was found. */ public function addBundleTitle(RouteMatchInterface $route_match, $entity_type_id, $bundle_parameter) { $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id); // If the entity has bundle entities, the parameter might have been upcasted // so fetch the raw parameter. $bundle = $route_match->getRawParameter($bundle_parameter); if (count($bundles) > 1 && isset($bundles[$bundle])) { return $this->t('Add @bundle', ['@bundle' => $bundles[$bundle]['label']]); } else { return $this->addTitle($entity_type_id); } }
/** * {@inheritdoc} */ public function renderItems($items) { if ($this->options['hide_single_bundle']) { $entity_type = $this->getEntityType(); $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type); // Hide bundle name if there's only one bundle. if (count($bundles) == 1) { return ''; } } return parent::renderItems($items); }
/** * The title callback for the add form. * * @param string $entity_type_id * The entity type ID. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * * @return string * The page title. */ public function addFormTitle($entity_type_id, RouteMatchInterface $route_match) { $entity_type = $this->entityTypeManager()->getDefinition($entity_type_id); $bundle_key = $entity_type->getKey('bundle'); $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id); if ($bundle_key && count($bundles) > 1) { $bundle_name = $route_match->getRawParameter($bundle_key); $title = $this->t('Add @bundle', ['@bundle' => $bundles[$bundle_name]['label']]); } else { $title = $this->t('Add @entity-type', ['@entity-type' => $entity_type->getLowercaseLabel()]); } return $title; }
/** * {@inheritDoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $options = []; foreach ($this->manager->getDefinitions() as $plugin_id => $plugin_definition) { $options[$plugin_id] = $plugin_definition['label']; } $form['type'] = ['#type' => 'select', '#title' => $this->t('Pattern type'), '#default_value' => $this->entity->getType(), '#options' => $options, '#required' => TRUE, '#limit_validation_errors' => array(array('type')), '#submit' => array('::submitSelectType'), '#executes_submit_callback' => TRUE, '#ajax' => array('callback' => '::ajaxReplacePatternForm', 'wrapper' => 'pathauto-pattern', 'method' => 'replace')]; $form['pattern_container'] = ['#type' => 'container', '#prefix' => '<div id="pathauto-pattern">', '#suffix' => '</div>']; // if there is no type yet, stop here. if ($this->entity->getType()) { $alias_type = $this->entity->getAliasType(); $form['pattern_container']['pattern'] = array('#type' => 'textfield', '#title' => 'Path pattern', '#default_value' => $this->entity->getPattern(), '#size' => 65, '#maxlength' => 1280, '#element_validate' => array('token_element_validate', 'pathauto_pattern_validate'), '#after_build' => array('token_element_validate'), '#token_types' => $alias_type->getTokenTypes(), '#min_tokens' => 1); // Show the token help relevant to this pattern type. $form['pattern_container']['token_help'] = array('#theme' => 'token_tree_link', '#token_types' => $alias_type->getTokenTypes()); // Expose bundle and language conditions. if ($alias_type->getDerivativeId() && ($entity_type = $this->entityTypeManager->getDefinition($alias_type->getDerivativeId()))) { $default_bundles = []; $default_languages = []; foreach ($this->entity->getSelectionConditions() as $condition_id => $condition) { if (in_array($condition->getPluginId(), ['entity_bundle:' . $entity_type->id(), 'node_type'])) { $default_bundles = $condition->getConfiguration()['bundles']; } elseif ($condition->getPluginId() == 'language') { $default_languages = $condition->getConfiguration()['langcodes']; } } if ($entity_type->hasKey('bundle') && ($bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type->id()))) { $bundle_options = []; foreach ($bundles as $id => $info) { $bundle_options[$id] = $info['label']; } $form['pattern_container']['bundles'] = array('#title' => $entity_type->getBundleLabel(), '#type' => 'checkboxes', '#options' => $bundle_options, '#default_value' => $default_bundles, '#description' => t('Check to which types this pattern should be applied. Leave empty to allow any.')); } if ($this->languageManager->isMultilingual() && $entity_type->isTranslatable()) { $language_options = []; foreach ($this->languageManager->getLanguages() as $id => $language) { $language_options[$id] = $language->getName(); } $form['pattern_container']['languages'] = array('#title' => $this->t('Languages'), '#type' => 'checkboxes', '#options' => $language_options, '#default_value' => $default_languages, '#description' => t('Check to which languages this pattern should be applied. Leave empty to allow any.')); } } } $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#maxlength' => 255, '#default_value' => $this->entity->label(), '#required' => TRUE); $form['id'] = array('#type' => 'machine_name', '#title' => $this->t('ID'), '#maxlength' => 255, '#default_value' => $this->entity->id(), '#required' => TRUE, '#disabled' => !$this->entity->isNew(), '#machine_name' => array('exists' => 'Drupal\\pathauto\\Entity\\PathautoPattern::load')); $form['status'] = ['#title' => $this->t('Enabled'), '#type' => 'checkbox', '#default_value' => $this->entity->status()]; return parent::buildForm($form, $form_state); }
/** * {@inheritdoc} */ public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); if ($this->targetEntityType->hasKey('bundle')) { $options = []; foreach ($this->entityTypeBundleInfo->getBundleInfo($this->targetEntityType->id()) as $bundle_id => $bundle_info) { $options[$bundle_id] = $bundle_info['label']; } $form['verf_target_bundles'] = [ '#type' => 'checkboxes', '#title' => $this->t('Target entity bundles to filter by'), '#options' => $options, '#default_value' => array_filter($this->options['verf_target_bundles']), ]; } return $form; }
/** * Writes the cache of type links. * * @param array $context * Context from the normalizer/serializer operation. * * @return array * An array of typed data ids (entity_type and bundle) keyed by * corresponding type URI. */ protected function writeCache($context = array()) { $data = array(); // Type URIs correspond to bundles. Iterate through the bundles to get the // URI and data for them. $entity_types = \Drupal::entityManager()->getDefinitions(); foreach ($this->bundleInfoService->getAllBundleInfo() as $entity_type_id => $bundles) { // Only content entities are supported currently. // @todo Consider supporting config entities. if ($entity_types[$entity_type_id]->isSubclassOf('\\Drupal\\Core\\Config\\Entity\\ConfigEntityInterface')) { continue; } foreach ($bundles as $bundle => $bundle_info) { // Get a type URI for the bundle. $bundle_uri = $this->getTypeUri($entity_type_id, $bundle, $context); $data[$bundle_uri] = array('entity_type' => $entity_type_id, 'bundle' => $bundle); } } // These URIs only change when entity info changes, so cache it permanently // and only clear it when entity_info is cleared. $this->cache->set('rest:links:types', $data, Cache::PERMANENT, array('entity_types')); return $data; }
/** * Builds the form structure for selecting the view's filters. * * By default, this adds "of type" and "tagged with" filters (when they are * available). */ protected function buildFilters(&$form, FormStateInterface $form_state) { module_load_include('inc', 'views_ui', 'admin'); $bundles = $this->bundleInfoService->getBundleInfo($this->entityTypeId); // If the current base table support bundles and has more than one (like user). if (!empty($bundles) && $this->entityType && $this->entityType->hasKey('bundle')) { // Get all bundles and their human readable names. $options = array('all' => $this->t('All')); foreach ($bundles as $type => $bundle) { $options[$type] = $bundle['label']; } $form['displays']['show']['type'] = array('#type' => 'select', '#title' => $this->t('of type'), '#options' => $options); $selected_bundle = static::getSelected($form_state, array('show', 'type'), 'all', $form['displays']['show']['type']); $form['displays']['show']['type']['#default_value'] = $selected_bundle; // Changing this dropdown updates the entire content of $form['displays'] // via AJAX, since each bundle might have entirely different fields // attached to it, etc. views_ui_add_ajax_trigger($form['displays']['show'], 'type', array('displays')); } }
/** * @covers ::getFieldMapByFieldType */ public function testGetFieldMapByFieldType() { // Set up a content entity type. $entity_type = $this->prophesize(ContentEntityTypeInterface::class); $entity_class = EntityManagerTestEntity::class; // Set up the entity type bundle info to return two bundles for the // fieldable entity type. $this->entityTypeBundleInfo->getBundleInfo('test_entity_type')->willReturn(['first_bundle' => 'first_bundle', 'second_bundle' => 'second_bundle'])->shouldBeCalled(); $this->moduleHandler->getImplementations('entity_base_field_info')->willReturn([])->shouldBeCalled(); // Define an ID field definition as a base field. $id_definition = $this->prophesize(FieldDefinitionInterface::class); $id_definition->getType()->willReturn('integer')->shouldBeCalled(); $base_field_definitions = ['id' => $id_definition->reveal()]; $entity_class::$baseFieldDefinitions = $base_field_definitions; // Set up the stored bundle field map. $key_value_store = $this->prophesize(KeyValueStoreInterface::class); $this->keyValueFactory->get('entity.definitions.bundle_field_map')->willReturn($key_value_store->reveal())->shouldBeCalled(); $key_value_store->getAll()->willReturn(['test_entity_type' => ['by_bundle' => ['type' => 'string', 'bundles' => ['second_bundle' => 'second_bundle']]]])->shouldBeCalled(); // Mock the base field definition override. $override_entity_type = $this->prophesize(EntityTypeInterface::class); $this->setUpEntityTypeDefinitions(['test_entity_type' => $entity_type, 'base_field_override' => $override_entity_type]); $entity_type->getClass()->willReturn($entity_class)->shouldBeCalled(); $entity_type->getKeys()->willReturn(['default_langcode' => 'default_langcode'])->shouldBeCalled(); $entity_type->isSubclassOf(FieldableEntityInterface::class)->willReturn(TRUE)->shouldBeCalled(); $entity_type->isTranslatable()->shouldBeCalled(); $entity_type->getProvider()->shouldBeCalled(); $override_entity_type->isSubclassOf(FieldableEntityInterface::class)->willReturn(FALSE)->shouldBeCalled(); $integerFields = $this->entityFieldManager->getFieldMapByFieldType('integer'); $this->assertCount(1, $integerFields['test_entity_type']); $this->assertArrayNotHasKey('non_fieldable', $integerFields); $this->assertArrayHasKey('id', $integerFields['test_entity_type']); $this->assertArrayNotHasKey('by_bundle', $integerFields['test_entity_type']); $stringFields = $this->entityFieldManager->getFieldMapByFieldType('string'); $this->assertCount(1, $stringFields['test_entity_type']); $this->assertArrayNotHasKey('non_fieldable', $stringFields); $this->assertArrayHasKey('by_bundle', $stringFields['test_entity_type']); $this->assertArrayNotHasKey('id', $stringFields['test_entity_type']); }
/** * Returns a bundle label. * * @param \Drupal\Core\Entity\RevisionableInterface $revision * The entity revision. * * @return string */ protected function getBundleLabel(RevisionableInterface $revision) { /** @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $revision */ $bundle_info = $this->bundleInformation->getBundleInfo($revision->getEntityTypeId()); return $bundle_info[$revision->bundle()]['label']; }
/** * {@inheritdoc} */ public function getFieldMap() { if (!$this->fieldMap) { // Not prepared, try to load from cache. $cid = 'entity_field_map'; if ($cache = $this->cacheGet($cid)) { $this->fieldMap = $cache->data; } else { // The field map is built in two steps. First, add all base fields, by // looping over all fieldable entity types. They always exist for all // bundles, and we do not expect to have so many different entity // types for this to become a bottleneck. foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { if ($entity_type->isSubclassOf(FieldableEntityInterface::class)) { $bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id)); foreach ($this->getBaseFieldDefinitions($entity_type_id) as $field_name => $base_field_definition) { $this->fieldMap[$entity_type_id][$field_name] = ['type' => $base_field_definition->getType(), 'bundles' => array_combine($bundles, $bundles)]; } } } // In the second step, the per-bundle fields are added, based on the // persistent bundle field map stored in a key value collection. This // data is managed in the EntityManager::onFieldDefinitionCreate() // and EntityManager::onFieldDefinitionDelete() methods. Rebuilding this // information in the same way as base fields would not scale, as the // time to query would grow exponentially with more fields and bundles. // A cache would be deleted during cache clears, which is the only time // it is needed, so a key value collection is used. $bundle_field_maps = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->getAll(); foreach ($bundle_field_maps as $entity_type_id => $bundle_field_map) { foreach ($bundle_field_map as $field_name => $map_entry) { if (!isset($this->fieldMap[$entity_type_id][$field_name])) { $this->fieldMap[$entity_type_id][$field_name] = $map_entry; } else { $this->fieldMap[$entity_type_id][$field_name]['bundles'] += $map_entry['bundles']; } } } $this->cacheSet($cid, $this->fieldMap, Cache::PERMANENT, ['entity_types']); } } return $this->fieldMap; }