page_tester-1.0.x-dev/src/Form/TestForm.php

src/Form/TestForm.php
<?php

declare(strict_types=1);

namespace Drupal\page_tester\Form;

use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;


/**
 * Form controller for the test entity edit forms.
 */
final class TestForm extends ContentEntityForm {

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

    $form['vocabulary']['widget']['#ajax'] = [
      'callback' => [$this, 'updateVocabularyFieldsCallback'],
      'wrapper' => 'vocabulary-fields-wrapper',
      'event' => 'change',
    ];

    $form['vocabulary']['#states'] = [
      'visible' => [
        ':input[name="test"]' => ['value' => 'tests_organigrams_tree'],
      ],
    ];

    $form['resource']['#states'] = [
      'visible' => [
        ':input[name="test"]' => ['value' => 'tests_organigrams_tree'],
      ],
    ];

    $form['vocabulary_fields']['#states'] = [
      'invisible' => [
        ':input[name="vocabulary"]' => ['value' => '_none'],
      ],
    ];
    $form['vocabulary_fields']['#prefix'] = '<div id="vocabulary-fields-wrapper">';
    $form['vocabulary_fields']['#suffix'] = '</div>';
    $form['vocabulary_fields']['#limit_validation_errors'] = [];

    $form['content_type']['widget']['#ajax'] = [
      'callback' => [$this, 'updateContentTypeLinksCallback'],
      'wrapper' => 'content-type-links-wrapper',
      'event' => 'change',
    ];

    $form['content_type']['#states'] = [
      'visible' => [
        ':input[name="test"]' => ['value' => 'tests_content_types'],
      ],
    ];

    $form['content_type_links']['#states'] = [
      'invisible' => [
        ':input[name="content_type"]' => ['value' => '_none'],
      ],
    ];
    $form['content_type_links']['#prefix'] = '<div id="content-type-links-wrapper">';
    $form['content_type_links']['#suffix'] = '</div>';
    $form['content_type_links']['#limit_validation_errors'] = [];

    $form['menu']['widget']['#ajax'] = [
      'callback' => [$this, 'updateMenuLinksCallback'],
      'wrapper' => 'menu-links-wrapper',
      'event' => 'change',
    ];

    $form['menu']['#states'] = [
      'visible' => [
        ':input[name="test"]' => ['value' => 'tests_menu_links'],
      ],
    ];

    $form['menu_links']['#states'] = [
      'invisible' => [
        ':input[name="menu"]' => ['value' => '_none'],
      ],
    ];
    $form['menu_links']['#prefix'] = '<div id="menu-links-wrapper">';
    $form['menu_links']['#suffix'] = '</div>';
    $form['menu_links']['#limit_validation_errors'] = [];

    if (!$form_state->hasValue('vocabulary')) {
      return $form;
    }

    $options = [];
    $vocabulary_id = $form_state->getValue('vocabulary')[0]['value'];
    if ($vocabulary_id != NULL) {
      $options = \Drupal::service('page_tester.field_definition_service')->getVocabularyFields($vocabulary_id);
    }

    $form['vocabulary_fields']['widget']['#options'] = $options;

    return $form;
  }

  public function updateVocabularyFieldsCallback(array &$form, FormStateInterface $form_state) {
    return $form['vocabulary_fields'];
  }

  public function updateContentTypeLinksCallback(array &$form, FormStateInterface $form_state) {
    return $form['content_type_links'];
  }

  public function updateMenuLinksCallback(array &$form, FormStateInterface $form_state) {
    return $form['menu_links'];
  }

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

    $message_args = ['%label' => $this->entity->toLink()->toString()];
    $logger_args = [
      '%label' => $this->entity->label(),
      'link' => $this->entity->toLink($this->t('View'))->toString(),
    ];

    switch ($result) {
      case SAVED_NEW:
        $this->messenger()->addStatus($this->t('New test %label has been created.', $message_args));
        $this->logger('page_tester')->notice('New test %label has been created.', $logger_args);
        break;

      case SAVED_UPDATED:
        $this->messenger()->addStatus($this->t('The test %label has been updated.', $message_args));
        $this->logger('page_tester')->notice('The test %label has been updated.', $logger_args);
        break;

      default:
        throw new \LogicException('Could not save the entity.');
    }

    $form_state->setRedirectUrl($this->entity->toUrl('collection'));

    return $result;
  }

}

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

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