/** * {@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 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); } }
/** * Gets the field definition. * * @return \Drupal\Core\Field\FieldDefinitionInterface */ protected function getFieldDefinition() { if (empty($this->fieldDefinition)) { $field_map = $this->entityFieldManager->getFieldMap(); $bundle = reset($field_map[$this->entityTypeId][$this->fieldName]['bundles']); $field_definitions = $this->entityFieldManager->getFieldDefinitions($this->entityTypeId, $bundle); $this->fieldDefinition = $field_definitions[$this->fieldName]; } return $this->fieldDefinition; }
/** * {@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; }
/** * 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} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); // Retrieve whether detailed option has been selected. $detailedOutput = $input->getOption('detailed'); // Retrieve whether an entity type has been specified. $entityTypeOption = $input->getOption('entity'); // Retrieve whether a specific bundle type has been specified. $bundleTypeOption = $input->getOption('bundle'); $entityList = $this->entityTypeManager->getDefinitions(); $allFields = $this->entityFieldManager->getFieldMap(); // Set a flag so we can error if a specific entity type selected but not found. $entityTypeOptionFound = false; // Set a flag so we can error if a specific bundle type selected but not found. $bundleTypeOptionFound = false; // Let's count the fields found so we can display a message if none found. $fieldCounter = 0; foreach ($entityList as $entityTypeId => $entityValue) { // If the Entity has bundleEntityType set we grab it. $bundleEntityType = $entityValue->get('bundle_entity_type'); // Check to see if the entity has any bundle before continuing. if (!empty($bundleEntityType)) { $bundleTypes = $this->entityTypeManager->getStorage($bundleEntityType)->loadMultiple(); // If a specific entity type has been selected and this is it then we continue else we skip. if ((!empty($entityTypeOption) && $entityTypeOption == $entityTypeId) | empty($entityTypeOption)) { // Store the fact that we found the entity type specified so we can error if not found. $entityTypeOptionFound = true; // Get the entity type label. $bundleParent = $entityValue->get('label'); // Using counter to know whether to output header. $bundleTypeCounter = 0; foreach ($bundleTypes as $bundleType) { // If a specific bundle type has been selected and this is it then we continue else we skip. if ((!empty($bundleTypeOption) && $bundleTypeOption == $bundleType->id()) | empty($bundleTypeOption)) { // Store the fact that we found the bundle type specified so we can error if not found. $bundleTypeOptionFound = true; // Increase the bundle type counter so we know whether to output header. $bundleTypeCounter++; if ($bundleTypeCounter == 1) { // Output the Parent Entity label if we haven't already. if ($detailedOutput) { // If detailed output then display the id as well. $io->info(strtoupper($bundleParent) . ' (' . $entityTypeId . '):'); } else { // otherwise just display the label for normal output. $io->info(strtoupper($bundleParent . ':')); } $io->newLine(); } // Load in the entityType fields. $fields = $this->getBundleFields($entityTypeId, $bundleType->id()); foreach ($fields as $field => $fieldArray) { // We found a field so increase the field counter. $fieldCounter++; // Get the related / used in bundles from the field. $relatedBundles = ""; $relatedBundlesArray = $allFields[$entityTypeId][$field]['bundles']; // Turn those related / used in bundles array into a string. foreach ($relatedBundlesArray as $relatedBundlesValue) { if ($bundleTypes[$relatedBundlesValue]->id() != $bundleType->id()) { if (!empty($relatedBundles)) { $relatedBundles .= ', ' . $bundleTypes[$relatedBundlesValue]->label(); } else { $relatedBundles = $bundleTypes[$relatedBundlesValue]->label(); } } } // Build out our table for the fields. $tableRows[] = $detailedOutput ? [$fieldArray->get('label'), $fieldArray->get('field_type'), $fieldArray->get('description'), $relatedBundles] : [$fieldArray->get('label'), $fieldArray->get('field_type'), $relatedBundles]; // Clear the related bundles ready for the next field. unset($relatedBundles); } // If detailed output then display bundle id and description. if ($detailedOutput) { // Output the bundle label and id. $io->info($bundleType->label() . ' (' . $bundleType->id() . ')'); $io->info(strip_tags($bundleType->get('description'))); } else { // Else just output the bundle label. $io->info($bundleType->label()); } // Fill out our table header. // If no rows exist for the fields then we display a no results message. if (!empty($tableRows)) { $tableHeader = $detailedOutput ? [$this->trans('commands.field.info.table.header-name'), $this->trans('commands.field.info.table.header-type'), $this->trans('commands.field.info.table.header-desc'), $this->trans('commands.field.info.table.header-usage')] : [$this->trans('commands.field.info.table.header-name'), $this->trans('commands.field.info.table.header-type'), $this->trans('commands.field.info.table.header-usage')]; $io->table($tableHeader, $tableRows); } else { $io->comment($this->trans('commands.field.info.messages.fields-none') . ' ' . $this->trans('commands.field.info.messages.in-bundle-type') . " '" . $bundleType->label() . "'"); } // Clear out the rows & headers arrays to start fresh. unset($tableHeader, $tableRows); // Create some space so the output looks nice. $io->newLine(); } } } } } // If entity type was specified but not found then display error message. if (!empty($entityTypeOption)) { if (!$entityTypeOptionFound) { $io->comment($this->trans('commands.field.info.messages.entity-type') . ' ' . $entityTypeOption . ' ' . $this->trans('commands.field.info.messages.not-found')); } elseif (!empty($bundleTypeOption) && !$bundleTypeOptionFound) { // If specified entity type found and bundle type specified but not found then display error message. $io->comment($this->trans('commands.field.info.messages.bundle-type') . ' ' . $bundleTypeOption . ' ' . $this->trans('commands.field.info.messages.not-found') . ' ' . $this->trans('commands.field.info.messages.in-entity-type') . ' ' . $entityTypeOption); } } elseif (!empty($bundleTypeOption) && !$bundleTypeOptionFound) { // If specified bundle type not found then display error message. $io->comment($this->trans('commands.field.info.messages.bundle-type') . ' ' . $bundleTypeOption . ' ' . $this->trans('commands.field.info.messages.not-found')); } elseif ($fieldCounter == 0) { // If no fields found then display appropriate message. $io->comment($this->trans('commands.field.info.messages.fields-none')); } return 0; }
/** * Determine the default that should be used to create default value elements. * * @param \Drupal\Core\Form\FormStateInterface $form_state * * @return string */ protected function getDefaultBundle($field_selected, FormStateInterface $form_state) { $all_fields = $this->entityFieldManager->getFieldMap(); $entity_fields = $all_fields[$this->entity->getUpdateEntityType()]; $bundle = array_shift(array_keys($entity_fields[$field_selected]['bundles'])); return $bundle; }