entity_agree-2.0.x-dev/src/Form/AgreementForm.php

src/Form/AgreementForm.php
<?php

namespace Drupal\entity_agree\Form;

use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Agreement form.
 */
class AgreementForm extends FormBase {

  /**
   * Agreement manager.
   *
   * @var \Drupal\entity_agree\AgreementManager
   */
  protected $agreementManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $form = parent::create($container);
    $form->agreementManager = $container->get('entity_agree.agreement_manager');
    return $form;
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, RevisionableInterface $entity = NULL, $configuration = []) {
    $form['#entity'] = $entity;

    $form['#view_mode'] = $configuration['view_mode'] ?? NULL;

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $configuration['button_text'] ?? $this->t('Agree'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->agreementManager->agree($form['#entity']);
    $this->messenger()->addMessage($this->t('You have agreed.'));
  }

}

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

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