diff --git a/content_model_documentation.services.yml b/content_model_documentation.services.yml index 6605184d849f1094370bb9046e1ef30d75764964..7dd5e8874aca98695188a5daed99fee718cfe905 100644 --- a/content_model_documentation.services.yml +++ b/content_model_documentation.services.yml @@ -15,6 +15,7 @@ services: - '@config.factory' - '@content_model_documentation.documentable.modules' - '@entity_field.manager' + - '@entity_type.manager' content_model_documentation.documentable.modules: class: Drupal\content_model_documentation\DocumentableModules arguments: diff --git a/src/DocumentableEntityProvider.php b/src/DocumentableEntityProvider.php index 9ac4ca2d52d9a3d6573550411ee3291c34314418..c4579877f6d9797780d58b0ca418e22748c7d5a2 100644 --- a/src/DocumentableEntityProvider.php +++ b/src/DocumentableEntityProvider.php @@ -7,6 +7,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Database\Database; use Drupal\Core\DependencyInjection\ServiceProviderBase; use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; /** * Service providing information on the Documentable entities. @@ -36,6 +37,13 @@ class DocumentableEntityProvider extends ServiceProviderBase { */ protected $entityFieldManager; + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + /** * An array of fieldmap. * @@ -52,11 +60,15 @@ class DocumentableEntityProvider extends ServiceProviderBase { * The documentable modules service. * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager * The time service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * The entity type manager. */ - public function __construct(ConfigFactoryInterface $configFactory, DocumentableModules $documentableModules, EntityFieldManagerInterface $entityFieldManager) { + public function __construct(ConfigFactoryInterface $configFactory, DocumentableModules $documentableModules, EntityFieldManagerInterface $entityFieldManager, EntityTypeManagerInterface $entityTypeManager) { $this->config = $configFactory->get('content_model_documentation.settings'); $this->entityFieldManager = $entityFieldManager; + $this->entityTypeManager = $entityTypeManager; $this->fieldMap = $this->entityFieldManager->getFieldMap(); + $this->fieldMap = $this->addMenusToFieldMap($this->fieldMap); $this->documentableModules = $documentableModules; } @@ -218,9 +230,43 @@ class DocumentableEntityProvider extends ServiceProviderBase { } } } - } return $documentable_entities; } + /** + * Optionally add menus to fieldMap if they are not there. + * + * @param array $field_map + * The fieldMap containing field-able entities. + * + * @return array + * Field map possibly with menus added. + */ + protected function addMenusToFieldMap(array $field_map): array { + // Determine if menus need to be added. + // If menus are field-able (example menu_item_extras is in use), then the + // menu entities will already be in the field map. + if (empty($field_map['menu_link_content']['uuid']['bundles']) || !empty($field_map['menu_link_content']['uuid']['bundles']['menu_link_content'])) { + // Menus are not in the field map so we need to add them. + $menu_storage = $this->entityTypeManager->getStorage('menu'); + $menus = $menu_storage->loadMultiple(); + $bundles = []; + $field_map['menu_link_content']['uuid']['bundles'] = []; + foreach ($menus as $menu) { + $bundles[$menu->id()] = $menu->id(); + } + $field_map['menu_link_content']['uuid']['bundles'] = $bundles; + // Also populate any fields + foreach ($field_map['menu_link_content'] as $element_name => $element) { + if ($this->isField($element_name)) { + // It is a field so add in the bundles. + $field_map['menu_link_content'][$element_name]['bundles'] = $bundles; + } + } + } + + return $field_map; + } + }