Skip to content
Snippets Groups Projects

#3465743 - do not allow selecting current display mode as fallback

1 file
+ 23
4
Compare changes
  • Side-by-side
  • Inline
@@ -8,6 +8,7 @@ use Drupal\Core\Field\FieldItemListInterface;
@@ -8,6 +8,7 @@ use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Link;
 
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\editablefields\services\EditableFieldsHelper;
use Drupal\editablefields\services\EditableFieldsHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -29,10 +30,17 @@ class EditableFieldsFieldFormatter extends FormatterBase {
@@ -29,10 +30,17 @@ class EditableFieldsFieldFormatter extends FormatterBase {
*/
*/
protected $editablefieldsHelper;
protected $editablefieldsHelper;
 
/**
 
* Current Route Match.
 
*
 
* @var \Drupal\Core\Routing\RouteMatchInterface
 
*/
 
protected $routerMatch;
 
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EditableFieldsHelper $editablefieldsHelper) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EditableFieldsHelper $editablefieldsHelper, RouteMatchInterface $route_match) {
parent::__construct(
parent::__construct(
$plugin_id,
$plugin_id,
$plugin_definition,
$plugin_definition,
@@ -43,6 +51,7 @@ class EditableFieldsFieldFormatter extends FormatterBase {
@@ -43,6 +51,7 @@ class EditableFieldsFieldFormatter extends FormatterBase {
$third_party_settings
$third_party_settings
);
);
$this->editablefieldsHelper = $editablefieldsHelper;
$this->editablefieldsHelper = $editablefieldsHelper;
 
$this->routerMatch = $route_match;
}
}
/**
/**
@@ -51,6 +60,8 @@ class EditableFieldsFieldFormatter extends FormatterBase {
@@ -51,6 +60,8 @@ class EditableFieldsFieldFormatter extends FormatterBase {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var \Drupal\editablefields\services\EditableFieldsHelper $editablefields_helper */
/** @var \Drupal\editablefields\services\EditableFieldsHelper $editablefields_helper */
$editablefields_helper = $container->get('editablefields.helper');
$editablefields_helper = $container->get('editablefields.helper');
 
/** @var \Drupal\Core\Routing\RouteMatchInterface $route_match */
 
$route_match = $container->get('current_route_match');
return new static(
return new static(
$plugin_id,
$plugin_id,
$plugin_definition,
$plugin_definition,
@@ -59,7 +70,8 @@ class EditableFieldsFieldFormatter extends FormatterBase {
@@ -59,7 +70,8 @@ class EditableFieldsFieldFormatter extends FormatterBase {
$configuration['label'],
$configuration['label'],
$configuration['view_mode'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$configuration['third_party_settings'],
$editablefields_helper
$editablefields_helper,
 
$route_match
);
);
}
}
@@ -85,6 +97,14 @@ class EditableFieldsFieldFormatter extends FormatterBase {
@@ -85,6 +97,14 @@ class EditableFieldsFieldFormatter extends FormatterBase {
*/
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
public function settingsForm(array $form, FormStateInterface $form_state) {
$entity_type_id = $this->fieldDefinition->getTargetEntityTypeId();
$entity_type_id = $this->fieldDefinition->getTargetEntityTypeId();
 
$fallback_modes = $this->editablefieldsHelper
 
->getViewModesOptions($entity_type_id);
 
$current_mode = $this->routerMatch->getParameter('view_mode_name');
 
if ($current_mode) {
 
// Do not allow using current display mode as fallback,
 
// to prevent loop.
 
unset($fallback_modes[$current_mode]);
 
}
return [
return [
'behaviour' => [
'behaviour' => [
'#type' => 'radios',
'#type' => 'radios',
@@ -130,8 +150,7 @@ class EditableFieldsFieldFormatter extends FormatterBase {
@@ -130,8 +150,7 @@ class EditableFieldsFieldFormatter extends FormatterBase {
'#type' => 'select',
'#type' => 'select',
'#title' => $this->t('Select no access display mode:'),
'#title' => $this->t('Select no access display mode:'),
'#default_value' => $this->getSetting('display_mode_access'),
'#default_value' => $this->getSetting('display_mode_access'),
'#options' => $this->editablefieldsHelper
'#options' => $fallback_modes,
->getViewModesOptions($entity_type_id),
'#description' => $this
'#description' => $this
->t('Use this formatter if user has no access to update entity.'),
->t('Use this formatter if user has no access to update entity.'),
'#states' => [
'#states' => [
Loading