entitytype_filter-1.0.x-dev/src/EntitySearchService.php
src/EntitySearchService.php
<?php
namespace Drupal\entitytype_filter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
/**
* Provides a helper functions for module.
*/
class EntitySearchService {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The field type plugin manager.
*
* @var \Drupal\Core\Field\FieldTypePluginManagerInterface
*/
protected $fieldTypePluginManager;
/**
* Constructs a new EntitySearchService object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_plugin_manager
* The field type plugin manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, FieldTypePluginManagerInterface $field_type_plugin_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->fieldTypePluginManager = $field_type_plugin_manager;
}
/**
* Retrieves a list of content entity type IDs.
*
* @return array
* An array of content entity type IDs.
*/
public function getBundleConfigEntityTypes($include_definition_data = FALSE): array {
$entity_definitions = $this->entityTypeManager->getDefinitions();
$config_entity_types = [];
$config_entity_definition_data = [];
$map = [
'taxonomy' => 'overview-form',
'eck' => 'collection',
];
foreach ($entity_definitions as $entity_type_id => $definition) {
if ($definition->get('group') == 'configuration' && $definition->get('bundle_of')) {
if ($include_definition_data) {
$provider = $definition->getProvider();
$links = $definition->get('links');
$path = $links['edit-form'];
if (isset($map[$provider])) {
switch ($provider) {
case 'taxonomy':
$path = $links['overview-form'];
break;
case 'eck':
$path = $links['collection'] . "/{" . $entity_type_id . "}";
break;
}
}
$config_entity_definition_data[$entity_type_id] = [
'label' => $definition->getLabel()->__toString(),
'label_key' => $definition->getKey('label'),
'path' => $path,
'bundle_of' => $definition->get('bundle_of'),
];
}
else {
$config_entity_types[] = $entity_type_id;
}
}
}
return $include_definition_data ? $config_entity_definition_data : $config_entity_types;
}
/**
* Recursively searches for a key in a nested array.
*/
public function recursiveSearchKeyMap($needle, $haystack) {
foreach ($haystack as $first_level_key => $value) {
if ($needle === $first_level_key) {
return [$first_level_key];
}
elseif (is_array($value)) {
$callback = $this->recursiveSearchKeyMap($needle, $value);
if ($callback) {
return ($value[$needle]->__toString());
}
}
}
return FALSE;
}
/**
* Returns grouped field type options by category.
*
* @return array
* A grouped array of field type labels keyed by category.
*/
public function getGroupedFieldTypeOptions(): array {
$field_type_options = [];
$grouped_definitions = $this->fieldTypePluginManager
->getGroupedDefinitions(
$this->fieldTypePluginManager->getUiDefinitions()
);
foreach ($grouped_definitions as $category => $field_types) {
foreach ($field_types as $name => $field_type) {
$field_type_options[$category][$name] = $field_type['label'];
}
}
return $field_type_options;
}
}
