You are here

trait LabeledFormTrait in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x src/Form/LabeledFormTrait.php \Drupal\xbbcode\Form\LabeledFormTrait

Helper function for adding label fields to an entity form.

@package Drupal\xbbcode\Form

Hierarchy

File

src/Form/LabeledFormTrait.php, line 13

Namespace

Drupal\xbbcode\Form
View source
trait LabeledFormTrait {
  use StringTranslationTrait;

  /**
   * Gets the form entity.
   *
   * The form entity which has been used for populating form element defaults.
   *
   * Redeclared here because PHP traits cannot implement interfaces.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The current form entity.
   *
   * @see \Drupal\Core\Entity\EntityFormInterface::getEntity()
   */
  public abstract function getEntity() : EntityInterface;

  /**
   * Add label fields to the form array.
   *
   * @param array $form
   *   Form array.
   *
   * @return array
   *   Form array.
   */
  public function addLabelFields(array $form) : array {
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#default_value' => $this
        ->getEntity()
        ->label(),
      '#maxlength' => 255,
      '#required' => TRUE,
      '#weight' => -30,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $this
        ->getEntity()
        ->id(),
      '#maxlength' => 255,
      '#machine_name' => [
        'exists' => [
          $this,
          'exists',
        ],
        'source' => [
          'label',
        ],
      ],
      '#disabled' => !$this
        ->getEntity()
        ->isNew(),
      '#weight' => -20,
    ];
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LabeledFormTrait::addLabelFields public function Add label fields to the form array.
LabeledFormTrait::getEntity abstract public function Gets the form entity. 2
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.