Issue #2933924 by lucienchalom, elber, smustgrave, tim.plunkett, larowlan, lauriii: Move \Drupal\field_ui\Form\EntityViewDisplayEditForm::getFieldLabelOptions() to a trait

merge-requests/4244/head
Lee Rowlands 2023-06-23 13:24:12 +10:00
parent 79c7666a86
commit 4aa426d9cc
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
3 changed files with 30 additions and 25 deletions

View File

@ -0,0 +1,25 @@
<?php
namespace Drupal\field;
/**
* Provides a trait for the valid field label options.
*/
trait FieldLabelOptionsTrait {
/**
* Returns an array of visibility options for field labels.
*
* @return array
* An array of visibility options.
*/
protected function getFieldLabelOptions(): array {
return [
'above' => $this->t('Above'),
'inline' => $this->t('Inline'),
'hidden' => '- ' . $this->t('Hidden') . ' -',
'visually_hidden' => '- ' . $this->t('Visually Hidden') . ' -',
];
}
}

View File

@ -8,6 +8,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\field_ui\FieldUI;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\field\FieldLabelOptionsTrait;
/**
* Edit form for the EntityViewDisplay entity type.
@ -15,7 +16,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* @internal
*/
class EntityViewDisplayEditForm extends EntityDisplayFormBase {
use FieldLabelOptionsTrait;
/**
* {@inheritdoc}
*/
@ -147,21 +148,6 @@ class EntityViewDisplayEditForm extends EntityDisplayFormBase {
] + FieldUI::getRouteBundleParameter($entity_type, $this->entity->getTargetBundle()));
}
/**
* Returns an array of visibility options for field labels.
*
* @return array
* An array of visibility options.
*/
protected function getFieldLabelOptions() {
return [
'above' => $this->t('Above'),
'inline' => $this->t('Inline'),
'hidden' => '- ' . $this->t('Hidden') . ' -',
'visually_hidden' => '- ' . $this->t('Visually Hidden') . ' -',
];
}
/**
* {@inheritdoc}
*/

View File

@ -23,6 +23,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\field\FieldLabelOptionsTrait;
/**
* Provides a block that renders a field from an entity.
@ -37,6 +38,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class FieldBlock extends BlockBase implements ContextAwarePluginInterface, ContainerFactoryPluginInterface {
use FieldLabelOptionsTrait;
/**
* The entity field manager.
*
@ -251,15 +253,7 @@ class FieldBlock extends BlockBase implements ContextAwarePluginInterface, Conta
$form['formatter']['label'] = [
'#type' => 'select',
'#title' => $this->t('Label'),
// @todo This is directly copied from
// \Drupal\field_ui\Form\EntityViewDisplayEditForm::getFieldLabelOptions(),
// resolve this in https://www.drupal.org/project/drupal/issues/2933924.
'#options' => [
'above' => $this->t('Above'),
'inline' => $this->t('Inline'),
'hidden' => '- ' . $this->t('Hidden') . ' -',
'visually_hidden' => '- ' . $this->t('Visually Hidden') . ' -',
],
'#options' => $this->getFieldLabelOptions(),
'#default_value' => $config['formatter']['label'],
];