Commit f0491512 authored by catch's avatar catch
Browse files

Issue #2899847 by Matroskeen, amateescu, Johnny vd Laar, alexpott: Add...

Issue #2899847 by Matroskeen, amateescu, Johnny vd Laar, alexpott: Add hook_entity_form_mode_alter hook to allow altering form_mode
parent 0b1816c9
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -81,6 +81,9 @@ public static function collectRenderDisplay(FieldableEntityInterface $entity, $f
    $entity_type = $entity->getEntityTypeId();
    $bundle = $entity->bundle();

    // Allow modules to change the form mode.
    \Drupal::moduleHandler()->alter('entity_form_mode', $form_mode, $entity);

    // Check the existence and status of:
    // - the display for the form mode,
    // - the 'default' display.
+17 −0
Original line number Diff line number Diff line
@@ -1820,6 +1820,23 @@ function hook_ENTITY_TYPE_prepare_form(\Drupal\Core\Entity\EntityInterface $enti
  }
}

/**
 * Change the form mode used to build an entity form.
 *
 * @param string $form_mode
 *   The form_mode that is to be used to build the entity form.
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity for which the form is being built.
 *
 * @ingroup entity_crud
 */
function hook_entity_form_mode_alter(&$form_mode, Drupal\Core\Entity\EntityInterface $entity) {
  // Change the form mode for users with Administrator role.
  if ($entity->getEntityTypeId() == 'user' && $entity->hasRole('administrator')) {
    $form_mode = 'my_custom_form_mode';
  }
}

/**
 * Alter the settings used for displaying an entity form.
 *
+9 −0
Original line number Diff line number Diff line
@@ -428,6 +428,15 @@ function entity_test_entity_field_access_alter(array &$grants, array $context) {
  }
}

/**
 * Implements hook_entity_form_mode_alter().
 */
function entity_test_entity_form_mode_alter(&$form_mode, EntityInterface $entity) {
  if ($entity->getEntityTypeId() === 'entity_test' && $entity->get('name')->value === 'compact_form_mode') {
    $form_mode = 'compact';
  }
}

/**
 * Implements hook_entity_form_display_alter().
 */
+36 −0
Original line number Diff line number Diff line
@@ -3,6 +3,9 @@
namespace Drupal\Tests\system\Functional\Entity;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;

@@ -57,6 +60,39 @@ public function testMultilingualFormCRUD() {
    }
  }

  /**
   * Tests hook_entity_form_mode_alter().
   *
   * @see entity_test_entity_form_mode_alter()
   */
  public function testEntityFormModeAlter() {
    // Create compact entity display.
    EntityFormMode::create(['id' => 'entity_test.compact', 'targetEntityType' => 'entity_test'])->save();
    EntityFormDisplay::create([
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'compact',
      'status' => TRUE,
    ])->removeComponent('field_test_text')->save();

    // The field should be available on default form mode.
    $entity1 = EntityTest::create([
      'name' => $this->randomString(),
    ]);
    $entity1->save();
    $this->drupalGet($entity1->toUrl('edit-form'));
    $this->assertSession()->elementExists('css', 'input[name="field_test_text[0][value]"]');

    // The field should be hidden on compact form mode.
    // See: entity_test_entity_form_mode_alter().
    $entity2 = EntityTest::create([
      'name' => 'compact_form_mode',
    ]);
    $entity2->save();
    $this->drupalGet($entity2->toUrl('edit-form'));
    $this->assertSession()->elementNotExists('css', 'input[name="field_test_text[0][value]"]');
  }

  /**
   * Tests hook_entity_form_display_alter().
   *