Unverified Commit 4f3432bf authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3448457 by narendrar, bbrala, smustgrave, alexpott, phenaproxima,...

Issue #3448457 by narendrar, bbrala, smustgrave, alexpott, phenaproxima, catch, borisson_, larowlan: Add validation constraints to core.entity_form_mode.*.*
parent ca674ff9
Loading
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -23,19 +23,36 @@ core.entity_view_mode.*.*:
core.entity_form_mode.*.*:
  type: config_entity
  label: 'Entity form mode settings'
  constraints:
    FullyValidatable: ~
  mapping:
    id:
      type: string
      label: 'ID'
      # Form mode IDs can only contain lowercase letters, numbers, and underscores
      # prefixed by entity type name this form mode is used for and a dot.
      # @see \Drupal\field_ui\Form\EntityDisplayModeFormBase::form()
      constraints:
        Regex:
          pattern: '/^[a-z0-9_]+\.[a-z0-9_]+$/'
          message: "The ID %value is not valid."
    label:
      type: required_label
      label: 'Label'
    description:
      type: text
      label: 'Description'
      nullable: true
      constraints:
        NotBlank:
          allowNull: true
    targetEntityType:
      type: string
      label: 'Target entity type'
      constraints:
        PluginExists:
          manager: entity_type.manager
          interface: Drupal\Core\Entity\ContentEntityInterface
    cache:
      type: boolean
      label: 'Cache'
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ abstract class EntityDisplayModeBase extends ConfigEntityBase implements EntityD
   *
   * @var string|null
   */
  protected ?string $description;
  protected ?string $description = NULL;

  /**
   * The entity type this form or view mode is used for.
+13 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
@@ -298,4 +299,16 @@ private function getDisplayByContext(string $bundle, string $display_mode_name):
    };
  }

  /**
   * {@inheritdoc}
   */
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state): void {
    // Config schema dictates that the description value
    // cannot be empty string. So, if it is empty, make it NULL.
    if ($form_state->hasValue('description') && trim($form_state->getValue('description')) === '') {
      $form_state->setValue('description', NULL);
    }
    parent::copyFormValuesToEntity($entity, $form, $form_state);
  }

}
+1 −0
Original line number Diff line number Diff line
@@ -133,6 +133,7 @@ public function testEntityFormModeUI(): void {
    $edit = [
      'id' => $this->randomMachineName(),
      'label' => $this->randomString(),
      'description' => $this->randomString(),
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains("Saved the {$edit['label']} form mode.");
+1 −0
Original line number Diff line number Diff line
@@ -270,6 +270,7 @@ public function testFormModeLocalTasksOrder(): void {
      'id' => 'node.big',
      'label' => 'Big Form',
      'targetEntityType' => 'node',
      'description' => 'Test description',
    ])->save();
    EntityFormMode::create([
      'id' => 'node.little',
Loading