Browse Source

Fix weights, and make prettier.

main
Rosie Le Faive 2 weeks ago
parent
commit
63f293d689
  1. 2
      css/metadata_profile.css
  2. 81
      src/Controller/MetadataProfileController.php

2
css/metadata_profile.css

@ -6,7 +6,7 @@
}
.field-info {
padding-bottom: 3rem;
border-bottom: gainsboro 1px solid;
border-bottom: gainsboro 2px solid;
margin-bottom: 3rem;
}
.field-info h2 {

81
src/Controller/MetadataProfileController.php

@ -5,6 +5,7 @@ namespace Drupal\metadata_profile\Controller;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
@ -14,6 +15,7 @@ use Drupal\Core\Link;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\node\NodeForm;
use Drupal\system\FileDownloadController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
@ -142,10 +144,8 @@ class MetadataProfileController extends ControllerBase {
'#type' => 'container',
'#weight' => '9',
];
// Refactor this to not loop; each thing called by displayField should use metadata profile.
$field_definitions = $this->entityFieldManager->getFieldDefinitions($this->entityTypeBundleOf, $this->entityBundle);
foreach ($field_definitions as $field_name => $field_definition) {
$build['fields'][$field_name] = $this->buildField($metadata_profile[$field_name]);
foreach ($metadata_profile as $field_name => $field_profile) {
$build['fields'][$field_name] = $this->buildField($field_profile);
}
return $build;
@ -227,7 +227,7 @@ class MetadataProfileController extends ControllerBase {
// * 'facet_machine_name'
// * 'facet_source'
// * 'url_alias'
// * 'block_visible'
// * 'block_visible' -- not yet implemented.
$metadata_profile = [];
$field_definitions = $this->entityFieldManager->getFieldDefinitions($this->entityTypeBundleOf, $this->entityBundle);
@ -249,6 +249,7 @@ class MetadataProfileController extends ControllerBase {
];
$metadata_profile[$field_name]['search_api'] = $this->getSearchApi($field_definition);
}
$metadata_profile = $this->addWeights($metadata_profile);
return $metadata_profile;
}
@ -350,6 +351,7 @@ class MetadataProfileController extends ControllerBase {
$display ? $this->formatListForTable($field_profile['target_bundles']) : $field_profile['target_bundles'],
$field_profile['search_api']['in_search_api'] ? 'Indexed' : '',
$field_profile['search_api']['has_facet'] ? 'Facet' : '',
$field_profile['weight'],
];
}
}
@ -368,6 +370,8 @@ class MetadataProfileController extends ControllerBase {
$this->t('Target bundles'),
$this->t('In Search API'),
$this->t('Has Facet'),
$this->t('Weight'),
// TODO: add more columns
];
}
@ -379,6 +383,7 @@ class MetadataProfileController extends ControllerBase {
$render_array = [
'#type' => 'container',
'#attributes' => ['class' => ['field-info']],
'#weight' => $field_profile['weight'],
// TODO: add weight here from form display defaults.
'title' => [
'#plain_text' => $field_profile['label'],
@ -396,16 +401,19 @@ class MetadataProfileController extends ControllerBase {
'#header' => ['Field Configuration',''],
'#rows' => $this->getFieldTableRows($field_profile),
'#attributes' => ['class' => ['field-info-table']],
'#prefix' => '<h3>' . $this->t('Field Information') . '</h3>',
],
'description' => [
'#type' => 'markup',
'#markup' => $field_profile['description'] ?: $this->t('[No description]'),
'#prefix' => '<b>Description:</b> ',
'#prefix' => '<h3>Description:</h3> ',
],
'search_api' => $this->formatSearchApi($field_profile),
'facets' => $this->formatFacets($field_profile),
];
}
return $render_array;
@ -493,8 +501,20 @@ class MetadataProfileController extends ControllerBase {
/**
* This does not work - most fields dont have a form weight specified in getDisplayOptions.
*/
protected function formatWeight(FieldDefinitionInterface $field_definition) {
return $field_definition->getDisplayOptions('form')['weight'];
protected function getWeight(string $field_name, array $form) {
if (isset($form[$field_name])) {
$local_weight = $form[$field_name]['#weight'];
if ($parent = $form['#group_children'][$field_name]) {
$parent_weight = $this->getWeight($parent, $form);
return $this->combine_weights($parent_weight, $local_weight);
}
else {
return $local_weight;
}
}
else {
return 9999;
}
}
protected function formatCardinality(FieldDefinitionInterface $field_definition) {
@ -511,7 +531,7 @@ class MetadataProfileController extends ControllerBase {
protected function formatCreateNew(FieldDefinitionInterface $field_definition) {
if (in_array($field_definition->getType(), ['entity_reference', 'typed_relation'])) {
$create_new_bundle = $field_definition->getSetting('handler_settings')['auto_create_bundle'] || '';
$create_new_bundle = $field_definition->getSetting('handler_settings')['auto_create_bundle'] ? $create_new_bundle = $field_definition->getSetting('handler_settings')['auto_create_bundle'] : '';
$create_new = $field_definition->getSetting('handler_settings')['auto_create'] || '';
if ($create_new) {
return $create_new_bundle ? 'Create new (' . $create_new_bundle . ')' : 'Create new';
@ -579,6 +599,7 @@ class MetadataProfileController extends ControllerBase {
'col6' => $this->t('Has facet'),
],
'#rows' => [],
'#prefix' => '<h3>' . $this->t('Search API fields') . '</h3>',
];
foreach ($field_profile['search_api']['fields'] as $search_api_field) {
$build['#rows'][] = $this->formatSearchApiField($search_api_field);
@ -709,6 +730,7 @@ class MetadataProfileController extends ControllerBase {
$this->t('Block is placed'),
],
'#rows' => [],
'#prefix' => '<h3>' . $this->t('Search Facets') . '</h3>',
];
foreach ($field_profile['search_api']['fields'] as $field) {
if ($field['has_facet']) {
@ -740,4 +762,45 @@ class MetadataProfileController extends ControllerBase {
];
}
private function addWeights(array $metadata_profile)
{
if ($this->entityTypeBundleOf == 'node') {
$node = \Drupal\node\Entity\Node::create(['type' => $this->entityBundle]);
$form = \Drupal::service('entity.form_builder')->getForm($node);
foreach($metadata_profile as $field_name => $field_profile) {
$metadata_profile[$field_name]['weight'] = $this->getWeight($field_name, $form);
}
$weights = array_column($metadata_profile, 'weight');
array_multisort($weights, SORT_ASC, $metadata_profile);
}
return $metadata_profile;
}
private function combine_weights($parent_weight, $local_weight)
{
$parent_int = (string)floor($parent_weight);
$parent_fraction = (string)($parent_weight - $parent_int);
$parent_fraction = str_replace('0.', '', $parent_fraction);
$local_int = (string)floor($local_weight);
$local_fraction = (string)($local_weight - $local_int);
$combined_weight = $parent_int;
$combined_weight .= '.';
if ($parent_fraction) {
# pad so its length is a multiple of 2
$length = mb_strlen($parent_fraction);
if (fmod($length, 2) != 0){
$parent_fraction .= '0';
}
$combined_weight .= $parent_fraction;
}
$combined_weight .= str_pad($local_int, 2, '0', STR_PAD_LEFT);
if ($local_fraction) {
$a=2;
}
return (float)$combined_weight;
}
}

Loading…
Cancel
Save