You are here

MollieProfileForm.php in Mollie Payment 8.2

File

src/Entity/MollieProfile/MollieProfileForm.php
View source
<?php

namespace Drupal\mollie_payment\Entity\MollieProfile;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface;
use Drupal\plugin\PluginType\PluginTypeManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the Mollie profile add/edit form.
 */
class MollieProfileForm extends EntityForm {

  /**
   * The Mollie profile storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $mollieProfileStorage;

  /**
   * The plugin selector manager.
   *
   * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface
   */
  protected $pluginSelectorManager;

  /**
   * The plugin type manager.
   *
   * @var \Drupal\plugin\PluginType\PluginTypeManager
   */
  protected $pluginTypeManager;

  /**
   * Constructs a new instance.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translator.
   * @param \Drupal\Core\Entity\EntityStorageInterface $mollie_profile_storage
   *   The Mollie profile storage.
   * @param \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface $plugin_selector_manager
   *   The plugin selector manager.
   * @param \Drupal\plugin\PluginType\PluginTypeManager $plugin_type_manager
   *   The plugin type manager.
   */
  public function __construct(TranslationInterface $string_translation, EntityStorageInterface $mollie_profile_storage, PluginSelectorManagerInterface $plugin_selector_manager, PluginTypeManager $plugin_type_manager) {
    $this->mollieProfileStorage = $mollie_profile_storage;
    $this->pluginSelectorManager = $plugin_selector_manager;
    $this->pluginTypeManager = $plugin_type_manager;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
    $entity_manager = $container
      ->get('entity.manager');
    return new static($container
      ->get('string_translation'), $entity_manager
      ->getStorage('mollie_profile'), $container
      ->get('plugin.manager.plugin.plugin_selector'), $container
      ->get('plugin.plugin_type_manager'));
  }

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

    /** @var \Drupal\mollie_payment\Entity\MollieProfilesInterface $mollie_profile */
    $mollie_profile = $this
      ->getEntity();
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#default_value' => $mollie_profile
        ->label(),
      '#maxlength' => 255,
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#default_value' => $mollie_profile
        ->id(),
      '#disabled' => !$mollie_profile
        ->isNew(),
      '#machine_name' => [
        'source' => [
          'label',
        ],
        'exists' => [
          $this,
          'MollieProfileIdExists',
        ],
      ],
      '#maxlength' => 255,
      '#type' => 'machine_name',
      '#required' => TRUE,
    ];
    $form['apiKey'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('API key'),
      '#default_value' => $mollie_profile
        ->getApiKey(),
      '#maxlength' => 255,
      '#required' => TRUE,
    ];
    return parent::form($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

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

  /**
   * {@inheritdoc}
   */
  protected function copyFormValuesToEntity(EntityInterface $mollie_profile, array $form, FormStateInterface $form_state) {

    /** @var \Drupal\mollie_payment\Entity\MolliePaymentInterface $mollie_profile */
    parent::copyFormValuesToEntity($mollie_profile, $form, $form_state);
    $values = $form_state
      ->getValues();
    $mollie_profile
      ->setId($values['id']);
    $mollie_profile
      ->setLabel($values['label']);
    $mollie_profile
      ->setApiKey($values['apiKey']);
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $mollie_profile = $this
      ->getEntity();
    $mollie_profile
      ->save();
    drupal_set_message($this
      ->t('@label has been saved.', [
      '@label' => $mollie_profile
        ->label(),
    ]));
    $form_state
      ->setRedirect('entity.mollie_profile.collection');
  }

  /**
   * Checks if a Mollie profile with a particular ID already exists.
   *
   * @param string $id
   *
   * @return bool
   */
  public function mollieProfileIdExists($id) {
    return (bool) $this->mollieProfileStorage
      ->load($id);
  }

}

Classes

Namesort descending Description
MollieProfileForm Provides the Mollie profile add/edit form.