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\Core\Url;
use Drupal\field_ui\FieldUI; use Drupal\field_ui\FieldUI;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\field\FieldLabelOptionsTrait;
/** /**
* Edit form for the EntityViewDisplay entity type. * Edit form for the EntityViewDisplay entity type.
@ -15,7 +16,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* @internal * @internal
*/ */
class EntityViewDisplayEditForm extends EntityDisplayFormBase { class EntityViewDisplayEditForm extends EntityDisplayFormBase {
use FieldLabelOptionsTrait;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -147,21 +148,6 @@ class EntityViewDisplayEditForm extends EntityDisplayFormBase {
] + FieldUI::getRouteBundleParameter($entity_type, $this->entity->getTargetBundle())); ] + 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} * {@inheritdoc}
*/ */

View File

@ -23,6 +23,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\field\FieldLabelOptionsTrait;
/** /**
* Provides a block that renders a field from an entity. * 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 { class FieldBlock extends BlockBase implements ContextAwarePluginInterface, ContainerFactoryPluginInterface {
use FieldLabelOptionsTrait;
/** /**
* The entity field manager. * The entity field manager.
* *
@ -251,15 +253,7 @@ class FieldBlock extends BlockBase implements ContextAwarePluginInterface, Conta
$form['formatter']['label'] = [ $form['formatter']['label'] = [
'#type' => 'select', '#type' => 'select',
'#title' => $this->t('Label'), '#title' => $this->t('Label'),
// @todo This is directly copied from '#options' => $this->getFieldLabelOptions(),
// \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') . ' -',
],
'#default_value' => $config['formatter']['label'], '#default_value' => $config['formatter']['label'],
]; ];