|
|
|
|
@ -11,6 +11,7 @@ use Drupal\Core\Field\FieldTypePluginManagerInterface;
|
|
|
|
|
use Drupal\Core\Link; |
|
|
|
|
use Drupal\Core\Messenger\MessengerTrait; |
|
|
|
|
use Drupal\Core\Config\Config; |
|
|
|
|
use Drupal\Core\Routing\RouteMatchInterface; |
|
|
|
|
use Drupal\Core\Url; |
|
|
|
|
use Drupal\field_permissions\Plugin\FieldPermissionType\Base; |
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
|
|
@ -24,20 +25,57 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
protected FieldTypePluginManagerInterface $fieldTypePluginManager; |
|
|
|
|
|
|
|
|
|
protected $configFactory; |
|
|
|
|
/** |
|
|
|
|
* The route matcher. |
|
|
|
|
* |
|
|
|
|
* @var \Drupal\Core\Routing\RouteMatchInterface |
|
|
|
|
*/ |
|
|
|
|
protected $routeMatch; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The entity type machine name. |
|
|
|
|
* |
|
|
|
|
* @var string |
|
|
|
|
*/ |
|
|
|
|
protected $entityType; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The bundle machine name. |
|
|
|
|
* |
|
|
|
|
* @var string |
|
|
|
|
*/ |
|
|
|
|
protected $entityBundle; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The entity type that our config entity describes bundles of. |
|
|
|
|
* |
|
|
|
|
* @var string |
|
|
|
|
*/ |
|
|
|
|
protected $entityTypeBundleOf; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct(EntityFieldManagerInterface $entity_field_manager, |
|
|
|
|
FieldTypePluginManagerInterface $field_type_plugin_manager, |
|
|
|
|
ConfigFactoryInterface $config_factory) { |
|
|
|
|
ConfigFactoryInterface $config_factory, |
|
|
|
|
RouteMatchInterface $route_match) { |
|
|
|
|
$this->entityFieldManager = $entity_field_manager; |
|
|
|
|
$this->fieldTypePluginManager = $field_type_plugin_manager; |
|
|
|
|
$this->configFactory = $config_factory; |
|
|
|
|
$this->routeMatch = $route_match; |
|
|
|
|
$route_options = $this->routeMatch->getRouteObject()->getOptions(); |
|
|
|
|
$array_keys = array_keys($route_options['parameters']); |
|
|
|
|
$this->entityType = array_shift($array_keys); |
|
|
|
|
$entity_type = $this->routeMatch->getParameter($this->entityType); |
|
|
|
|
$this->entityBundle = $entity_type->id(); |
|
|
|
|
$this->entityTypeBundleOf = $entity_type->getEntityType()->getBundleOf(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static function create(ContainerInterface $container) { |
|
|
|
|
return new static( |
|
|
|
|
$container->get('entity_field.manager'), |
|
|
|
|
$container->get('plugin.manager.field.field_type'), |
|
|
|
|
$container->get('config.factory') |
|
|
|
|
$container->get('config.factory'), |
|
|
|
|
$container->get('current_route_match') |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -47,14 +85,16 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
* @param $content_type |
|
|
|
|
* @return array |
|
|
|
|
*/ |
|
|
|
|
public function profile($content_type) { |
|
|
|
|
|
|
|
|
|
public function profile($content_type = NULL) { |
|
|
|
|
if (!$content_type) { |
|
|
|
|
$content_type = $this->entityBundle; |
|
|
|
|
} |
|
|
|
|
// Get core content type information. |
|
|
|
|
$node_type = $this->config('node.type.' . $content_type); |
|
|
|
|
if (!$node_type) { |
|
|
|
|
$bundle_config = $this->config(str_replace('_', '.', $this->entityType) . '.' . $content_type); |
|
|
|
|
if (!$bundle_config) { |
|
|
|
|
return ['#markup'=> 'Not a valid content type.']; |
|
|
|
|
} |
|
|
|
|
$build = $this->format_bundle($node_type); |
|
|
|
|
$build = $this->format_bundle($bundle_config); |
|
|
|
|
|
|
|
|
|
$metadata_profile = $this->getMetadataProfile($content_type); |
|
|
|
|
|
|
|
|
|
@ -69,7 +109,7 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
'#weight' => '9', |
|
|
|
|
]; |
|
|
|
|
// Get field information. |
|
|
|
|
$field_definitions = $this->entityFieldManager->getFieldDefinitions('node', $content_type); |
|
|
|
|
$field_definitions = $this->entityFieldManager->getFieldDefinitions($this->entityTypeBundleOf, $content_type); |
|
|
|
|
foreach ($field_definitions as $field_name => $field_definition) { |
|
|
|
|
$build['fields'][$field_name] = $this->display_field($field_name, $field_definition, $metadata_profile[$field_name]); |
|
|
|
|
} |
|
|
|
|
@ -81,22 +121,38 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
* Format information about a bundle. |
|
|
|
|
*/ |
|
|
|
|
protected function format_bundle(Config $bundle) { |
|
|
|
|
if (str_starts_with($bundle->getName(), 'node.type')) { |
|
|
|
|
$label = $bundle->get('name'); |
|
|
|
|
$machine_name = $bundle->get('type'); |
|
|
|
|
$description = $bundle->get('description'); |
|
|
|
|
} |
|
|
|
|
elseif (str_starts_with($bundle->getName(), 'taxonomy.vocabulary')) { |
|
|
|
|
$label = $bundle->get('name'); |
|
|
|
|
$machine_name = $bundle->get('vid'); |
|
|
|
|
$description = $bundle->get('description'); |
|
|
|
|
} |
|
|
|
|
elseif (str_starts_with($bundle->getName(), 'media.type')) { |
|
|
|
|
$label = $bundle->get('label'); |
|
|
|
|
$machine_name = $bundle->get('id'); |
|
|
|
|
$description = $bundle->get('description'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$build = [ |
|
|
|
|
'#type' => 'container', |
|
|
|
|
'#attached' => ['library' => ['metadata_profile/metadata_profile']], |
|
|
|
|
|
|
|
|
|
'title' => [ |
|
|
|
|
'#plain_text' => $bundle->get('name'), |
|
|
|
|
'#plain_text' => $label, |
|
|
|
|
'#prefix' => '<h1>', |
|
|
|
|
'#suffix' => '</h1>', |
|
|
|
|
], |
|
|
|
|
'machine_name' => [ |
|
|
|
|
'#plain_text' => $bundle->get('type'), |
|
|
|
|
'#plain_text' => $machine_name, |
|
|
|
|
'#prefix' => ' (', |
|
|
|
|
'#suffix' => ')', |
|
|
|
|
], |
|
|
|
|
'description' => [ |
|
|
|
|
'#plain_text' => $bundle->get('description'), |
|
|
|
|
'#plain_text' => $description, |
|
|
|
|
'#prefix' => '<p>', |
|
|
|
|
'#suffix' => '</p>', |
|
|
|
|
], |
|
|
|
|
@ -137,7 +193,7 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
// * 'block_visible' |
|
|
|
|
|
|
|
|
|
$metadata_profile = []; |
|
|
|
|
$field_definitions = $this->entityFieldManager->getFieldDefinitions('node', $bundle); |
|
|
|
|
$field_definitions = $this->entityFieldManager->getFieldDefinitions($this->entityTypeBundleOf, $bundle); |
|
|
|
|
foreach ($field_definitions as $field_name => $field_definition) { |
|
|
|
|
$metadata_profile[$field_name] = [ |
|
|
|
|
'label' => $field_definition->getLabel(), |
|
|
|
|
@ -160,7 +216,7 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
protected function buildSummaryTable($metadata_profile) { |
|
|
|
|
$rows = []; |
|
|
|
|
foreach ($metadata_profile as $field_name => $field_profile) { |
|
|
|
|
if (str_starts_with( $field_name, 'field_') or $field_name == 'title') { |
|
|
|
|
if (str_starts_with( $field_name, 'field_') or in_array($field_name, ['title', 'name'])) { |
|
|
|
|
$rows[] = [ |
|
|
|
|
$field_profile['details_link'], |
|
|
|
|
$field_profile['machine_name'], |
|
|
|
|
@ -192,7 +248,7 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
protected function display_field(string $field_name, FieldDefinitionInterface $field_definition, array $field_profile) { |
|
|
|
|
$render_array = []; |
|
|
|
|
|
|
|
|
|
if (str_starts_with($field_name, 'field_') or $field_name == 'title') { |
|
|
|
|
if (str_starts_with($field_name, 'field_') or in_array($field_name, ['title', 'name'])) { |
|
|
|
|
$render_array = [ |
|
|
|
|
'#type' => 'container', |
|
|
|
|
'#attributes' => ['class' => ['field-info']], |
|
|
|
|
@ -232,8 +288,8 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
if ($field_definition->getFieldStorageDefinition() instanceof BaseFieldDefinition) { |
|
|
|
|
if ($this->moduleHandler()->moduleExists('base_field_override_ui')) { |
|
|
|
|
if (method_exists($field_definition, 'id')) { |
|
|
|
|
$edit_url = Url::fromRoute('entity.base_field_override.node_base_field_override_edit_form', [ |
|
|
|
|
'node_type' => $field_definition->getTargetBundle(), |
|
|
|
|
$edit_url = Url::fromRoute('entity.base_field_override.' . $this->entityTypeBundleOf . '_base_field_override_edit_form', [ |
|
|
|
|
$this->entityType => $field_definition->getTargetBundle(), |
|
|
|
|
'base_field_override' => $field_definition->id(), |
|
|
|
|
'destination' => $redirect_url->toString() |
|
|
|
|
]); |
|
|
|
|
@ -248,8 +304,8 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
$edit_url = Url::fromRoute('entity.field_config.node_field_edit_form', [ |
|
|
|
|
'node_type' => $field_definition->getTargetBundle(), |
|
|
|
|
$edit_url = Url::fromRoute('entity.field_config.' . $this->entityTypeBundleOf . '_field_edit_form', [ |
|
|
|
|
$this->entityType => $field_definition->getTargetBundle(), |
|
|
|
|
'field_config' => $field_definition->id(), |
|
|
|
|
'destination' => $redirect_url->toString(), |
|
|
|
|
]); |
|
|
|
|
@ -386,7 +442,7 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
$storage = $field_definition->getFieldStorageDefinition(); |
|
|
|
|
if ($storage instanceof BaseFieldDefinition) { |
|
|
|
|
// Use property_path to find related fields. |
|
|
|
|
$field_id = 'node.' . $storage->getName(); |
|
|
|
|
$field_id = $this->entityTypeBundleOf . '.' . $storage->getName(); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
$field_id = 'field.storage.' . $storage->get('id'); |
|
|
|
|
@ -412,6 +468,7 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
$build['#rows'][$field_setting_name] = $this->format_search_api_field($field_setting_name, $field_setting, $index_config); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if ($field_definition->getType() == 'edtf') { |
|
|
|
|
// Get EDTF year. |
|
|
|
|
if ($field_setting['property_path'] == 'edtf_year') { |
|
|
|
|
$edtf_year_fields = $index_config->get('processor_settings')['edtf_year_only']['fields'] ?: []; |
|
|
|
|
@ -422,10 +479,11 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
// Get EDTF Date |
|
|
|
|
if ($field_setting['property_path'] == 'edtf_dates') { |
|
|
|
|
$edtf_date_fields = $index_config->get('processor_settings')['edtf_date_processor']['fields'] ?: []; |
|
|
|
|
if (in_array(str_replace('.','|',$field_definition->id()), $edtf_date_fields)) { |
|
|
|
|
if (in_array(str_replace('.', '|', $field_definition->id()), $edtf_date_fields)) { |
|
|
|
|
$build['#rows'][$field_setting_name] = $this->format_search_api_field($field_setting_name, $field_setting, $index_config); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
if (count($build['#rows']) == 0) { |
|
|
|
|
@ -478,33 +536,6 @@ class MetadataProfileController extends ControllerBase {
|
|
|
|
|
return $build; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function test() { |
|
|
|
|
$list = [ |
|
|
|
|
'one', |
|
|
|
|
'two', |
|
|
|
|
'three' |
|
|
|
|
]; |
|
|
|
|
$list_render_array = [ |
|
|
|
|
'#theme' => 'item_list', |
|
|
|
|
'#items' => $list, |
|
|
|
|
]; |
|
|
|
|
$test = [ |
|
|
|
|
'data' => $list_render_array |
|
|
|
|
]; |
|
|
|
|
$rows = [ |
|
|
|
|
['bar', 'xyz'], |
|
|
|
|
['baz', $test ], |
|
|
|
|
|
|
|
|
|
]; |
|
|
|
|
$build = [ |
|
|
|
|
'#type' => 'table', |
|
|
|
|
'#rows' => $rows, |
|
|
|
|
|
|
|
|
|
]; |
|
|
|
|
return $build; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function getDetailsFragmentLink(string $field_name, FieldDefinitionInterface $field_definition) |
|
|
|
|
{ |
|
|
|
|
$url = Url::fromRoute('<current>', [], ['fragment' => $field_name]); |
|
|
|
|
|