jfu-1.0.x-dev/src/Form/JfuTextFormatDialog.php

src/Form/JfuTextFormatDialog.php
<?php

namespace Drupal\jfu\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\jfu\Services\JfuConfigServices;
use Drupal\jfu\Ajax\JfuDialogSave;

/**
 * Provides an image dialog for json field utils.
 *
 * @internal
 */
class JfuTextFormatDialog extends FormBase {
  /**
   * The entity Config Factory.
   *
   * @var Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected EntityTypeManagerInterface $entityTypeManager;

  /**
   * The Jfu config.
   *
   * @var Drupal\jfu\Ajax\JfuDialogSave
   */
  protected $jfuConfig;


  /**
   * Constructs a form object for image dialog.
   *
   * @param \Drupal\Core\Entity\ConfigFactory $config_factory
   *   The file storage service.
   * @param Drupal\jfu\Services\EntityTypeManagerInterface $jfu_entity_type_manager
   *   The The entity type manager.
   * @param \Drupal\Core\Entity\JfuConfigServices $jfu_config
   *   The file storage service.
   */
  public function __construct(ConfigFactory $config_factory, EntityTypeManagerInterface $jfu_entity_type_manager, JfuConfigServices $jfu_config) {
    $this->configFactory = $config_factory;
    $this->entityTypeManager = $jfu_entity_type_manager;
    $this->jfuConfig = $jfu_config;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('entity_type.manager'),
      $container->get('jfu_config.service')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'jfu_text_format_dialog';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $attributes = $this->getRequest()->attributes->all();
    $query = $this->getRequest()->query->all();
    $entity_type = $attributes['entity_type'];
    $bundle = $attributes['bundle'];
    $field_name = $query['field_name'];
    $format = !empty($query['data_format']) ? $query['data_format'] : 'basic_html';

    $form['jfu_body'] = [
      '#type' => 'text_format',
      '#default_value' => '',
      '#title' => t('Description'),
      '#format'=> $format !== 'advance_html' ? $format : 'basic_html',
    ];

    $jfu_body_subindex = "";
    if(isset($query['subindex']) && $query['subindex'] != 'undefined' && $query['subindex'] != '') {
      $jfu_body_subindex = $query['subindex'];
    }

    $form['jfu_body_subindex'] = [
      '#type' => 'hidden',
      '#value' => $jfu_body_subindex,
    ];

    if (isset($query['index']) && $query['index'] != '') {
      $form['jfu_body_index'] = [
        '#type' => 'hidden',
        '#value' => $query['index'],
      ];
    }

    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['save_modal'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
      '#submit' => [],
      '#ajax' => [
        'callback' => '::submitForm',
        'event' => 'click',
      ],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $response = new AjaxResponse();

    if ($form_state->getErrors()) {
      unset($form['#prefix'], $form['#suffix']);
      $form['status_messages'] = [
        '#type' => 'status_messages',
        '#weight' => -10,
      ];
      $response->addCommand(new HtmlCommand('#jfu-text-format-dialog-form', $form));
    }
    else {
      $response->addCommand(new JfuDialogSave($form_state->getValues()));
      $response->addCommand(new CloseModalDialogCommand());
    }

    return $response;
  }

}

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

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