From 0c6437ac313527bff0c3564be376a68e20d3376d Mon Sep 17 00:00:00 2001
From: Oleksandr Akerman <o.akerman@dev-branch.com>
Date: Sat, 25 Jan 2025 13:49:16 +0200
Subject: [PATCH] #3465743 - do not allow selecting current display mode as
 fallback

---
 .../EditableFieldsFieldFormatter.php          | 27 ++++++++++++++++---
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/src/Plugin/Field/FieldFormatter/EditableFieldsFieldFormatter.php b/src/Plugin/Field/FieldFormatter/EditableFieldsFieldFormatter.php
index bb41fcc..f7d270f 100644
--- a/src/Plugin/Field/FieldFormatter/EditableFieldsFieldFormatter.php
+++ b/src/Plugin/Field/FieldFormatter/EditableFieldsFieldFormatter.php
@@ -8,6 +8,7 @@ use Drupal\Core\Field\FieldItemListInterface;
 use Drupal\Core\Field\FormatterBase;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Link;
+use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\editablefields\services\EditableFieldsHelper;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
@@ -29,10 +30,17 @@ class EditableFieldsFieldFormatter extends FormatterBase {
    */
   protected $editablefieldsHelper;
 
+  /**
+   * Current Route Match.
+   *
+   * @var \Drupal\Core\Routing\RouteMatchInterface
+   */
+  protected $routerMatch;
+
   /**
    * {@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(
       $plugin_id,
       $plugin_definition,
@@ -43,6 +51,7 @@ class EditableFieldsFieldFormatter extends FormatterBase {
       $third_party_settings
     );
     $this->editablefieldsHelper = $editablefieldsHelper;
+    $this->routerMatch = $route_match;
   }
 
   /**
@@ -51,6 +60,8 @@ class EditableFieldsFieldFormatter extends FormatterBase {
   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
     /** @var \Drupal\editablefields\services\EditableFieldsHelper $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(
       $plugin_id,
       $plugin_definition,
@@ -59,7 +70,8 @@ class EditableFieldsFieldFormatter extends FormatterBase {
       $configuration['label'],
       $configuration['view_mode'],
       $configuration['third_party_settings'],
-      $editablefields_helper
+      $editablefields_helper,
+      $route_match
     );
   }
 
@@ -85,6 +97,14 @@ class EditableFieldsFieldFormatter extends FormatterBase {
    */
   public function settingsForm(array $form, FormStateInterface $form_state) {
     $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 [
         'behaviour' => [
           '#type' => 'radios',
@@ -130,8 +150,7 @@ class EditableFieldsFieldFormatter extends FormatterBase {
           '#type' => 'select',
           '#title' => $this->t('Select no access display mode:'),
           '#default_value' => $this->getSetting('display_mode_access'),
-          '#options' => $this->editablefieldsHelper
-            ->getViewModesOptions($entity_type_id),
+          '#options' => $fallback_modes,
           '#description' => $this
             ->t('Use this formatter if user has no access to update entity.'),
           '#states' => [
-- 
GitLab