You are here

trait EntityDescriptionFormTrait in Lightning Core 8.2

Same name and namespace in other branches
  1. 8.5 src/EntityDescriptionFormTrait.php \Drupal\lightning_core\EntityDescriptionFormTrait
  2. 8 src/EntityDescriptionFormTrait.php \Drupal\lightning_core\EntityDescriptionFormTrait
  3. 8.3 src/EntityDescriptionFormTrait.php \Drupal\lightning_core\EntityDescriptionFormTrait
  4. 8.4 src/EntityDescriptionFormTrait.php \Drupal\lightning_core\EntityDescriptionFormTrait

Adds description support to entity forms.

Hierarchy

4 files declare their use of EntityDescriptionFormTrait
EntityDisplayModeAddForm.php in src/Form/EntityDisplayModeAddForm.php
EntityDisplayModeEditForm.php in src/Form/EntityDisplayModeEditForm.php
EntityFormModeAddForm.php in src/Form/EntityFormModeAddForm.php
RoleForm.php in src/Form/RoleForm.php

File

src/EntityDescriptionFormTrait.php, line 10

Namespace

Drupal\lightning_core
View source
trait EntityDescriptionFormTrait {

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\EntityDescriptionInterface $entity */
    $entity = $this
      ->getEntity();
    $form = parent::form($form, $form_state);
    $form['description'] = [
      '#type' => 'textarea',
      '#title' => t('Description'),
      '#description' => $this
        ->t('Additional relevant information about this @entity_type, such as where it is used and what it is for.', [
        '@entity_type' => $entity
          ->getEntityType()
          ->getSingularLabel(),
      ]),
      '#rows' => 2,
      '#default_value' => $entity
        ->getDescription(),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $this
      ->getEntity()
      ->setDescription($form_state
      ->getValue('description'));
    parent::save($form, $form_state);

    // The help text block is very likely to be render cached, so invalidate the
    // relevant cache tag.
    //
    // @see lightning_core_block_view_alter()
    // @see lightning_core_help().
    $this
      ->cacheTagInvalidator()
      ->invalidateTags([
      'block_view:help_block',
    ]);
  }

  /**
   * Returns the cache tag invalidator service.
   *
   * @return \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   *   The cache tag invalidator.
   */
  private function cacheTagInvalidator() {
    return @$this->cacheTagInvalidator ?: \Drupal::service('cache_tags.invalidator');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityDescriptionFormTrait::cacheTagInvalidator private function Returns the cache tag invalidator service.
EntityDescriptionFormTrait::form public function
EntityDescriptionFormTrait::save public function