htools-8.x-1.x-dev/modules/htools_relations/src/Form/RelationalEntityTypeForm.php

modules/htools_relations/src/Form/RelationalEntityTypeForm.php
<?php

namespace Drupal\htools_relations\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class RelationalEntityTypeForm.
 */
class RelationalEntityTypeForm extends EntityForm {

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

    $relational_entity_type = $this->entity;
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $relational_entity_type->label(),
      '#description' => $this->t("Label for the Relational entity type."),
      '#required' => TRUE,
    ];

    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $relational_entity_type->id(),
      '#machine_name' => [
        'exists' => '\Drupal\htools_relations\Entity\RelationalEntityType::load',
      ],
      '#disabled' => !$relational_entity_type->isNew(),
    ];

    /* You will need additional form elements for your custom properties. */

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $relational_entity_type = $this->entity;
    $status = $relational_entity_type->save();

    switch ($status) {
      case SAVED_NEW:
        $this->messenger()->addMessage($this->t('Created the %label Relational entity type.', [
          '%label' => $relational_entity_type->label(),
        ]));
        break;

      default:
        $this->messenger()->addMessage($this->t('Saved the %label Relational entity type.', [
          '%label' => $relational_entity_type->label(),
        ]));
    }
    $form_state->setRedirectUrl($relational_entity_type->toUrl('collection'));
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc