marketo_suite-1.0.x-dev/src/Entity/Form/MarketoFormEntityForm.php

src/Entity/Form/MarketoFormEntityForm.php
<?php

namespace Drupal\e3_marketo\Entity\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\e3_marketo\Plugin\MarketoHandlerManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Form controller for Marketo form entity edit forms.
 *
 * @ingroup e3_marketo
 */
class MarketoFormEntityForm extends ContentEntityForm {

  /**
   * Marketo Handlers Manager.
   *
   * @var \Drupal\e3_marketo\Plugin\MarketoHandlerManager
   */
  protected MarketoHandlerManager $marketoHandlerManager;

  /**
   * Constructs a MarketoFormEntityForm object.
   *
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository service.
   * @param \Drupal\e3_marketo\Plugin\MarketoHandlerManager $marketo_handler_manager
   *   Marketo handler manager service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface|null $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\Component\Datetime\TimeInterface|null $time
   *   The time service.
   */
  public function __construct(
    EntityRepositoryInterface $entity_repository,
    MarketoHandlerManager $marketo_handler_manager,
    EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL,
    TimeInterface $time = NULL
  ) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);

    $this->marketoHandlerManager = $marketo_handler_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) : static {
    return new static(
      $container->get('entity.repository'),
      $container->get('plugin.manager.marketo_handler_manager'),
      $container->get('entity_type.bundle.info'),
      $container->get('datetime.time')
    );
  }

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

    /** @var \Drupal\e3_marketo\Entity\MarketoFormEntity $entity */
    $entity = $this->entity;

    $form['#title'] = $this->t('@operation @title Marketo Form', [
      '@operation' => ucfirst($operation),
      '@title' => $operation === 'add' ? $this->getBundleEntity()->label() : $entity->getName(),
    ]);

    if (!$this->entity->isNew()) {
      $form['new_revision'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Create new revision'),
        '#default_value' => FALSE,
        '#weight' => 10,
      ];

      $form['revision_log']['#states'] = [
        'visible' => [
          'input[name="new_revision"]' => ['checked' => TRUE],
        ],
      ];
    }
    else {
      $form['revision_log']['#access'] = FALSE;
    }

    // Process core submission behaviors.
    if (isset($form['submission_behavior'])) {
      if (isset($form['field_submission_confirmation'])) {
        $form['field_submission_confirmation']['#states'] = [
          'visible' => [
            'select[name="submission_behavior"]' => ['value' => 'msb_replace'],
          ],
        ];
      }

      if (isset($form['field_redirect_url'])) {
        $form['field_redirect_url']['#states'] = [
          'visible' => [
            'select[name="submission_behavior"]' => ['value' => 'msb_redirect'],
          ],
        ];
      }
    }

    // Invoke all alterMarketoEntityForm callbacks on applicable handlers.
    $this->marketoHandlerManager->invokeHandlers($entity, 'alterMarketoEntityForm', $form);
    return $form;
  }

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

    // Save as a new revision if requested to do so.
    if (!$form_state->isValueEmpty('new_revision') && $form_state->getValue('new_revision')) {
      $entity->setNewRevision();

      // If a new revision is created, save the current user as revision author.
      $entity->setRevisionCreationTime($this->time->getRequestTime());
      $entity->setRevisionUserId($this->currentUser()->id());
    }
    else {
      $entity->setNewRevision(FALSE);
    }

    $status = parent::save($form, $form_state);
    switch ($status) {
      case SAVED_NEW:
        $this->messenger()->addStatus($this->t('Created the @label Marketo form entity.', [
          '@label' => $entity->label(),
        ]));
        break;

      default:
        $this->messenger()->addStatus($this->t('Saved the @label Marketo form entity.', [
          '@label' => $entity->label(),
        ]));
    }

    $form_state->setRedirect('view.marketo_forms.marketo_forms_list');
    return $entity->id();
  }

}

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

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