You are here

class EncryptionProfileForm in Encrypt 8.3

Provides the form to add / edit an EncryptionProfile entity.

@package Drupal\encrypt\Form

Hierarchy

Expanded class hierarchy of EncryptionProfileForm

File

src/Form/EncryptionProfileForm.php, line 18

Namespace

Drupal\encrypt\Form
View source
class EncryptionProfileForm extends EntityForm {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * The EncryptService definition.
   *
   * @var \Drupal\encrypt\EncryptService
   */
  protected $encryptService;

  /**
   * Keeps track of extra confirmation step on profile edit.
   *
   * @var bool
   */
  protected $editConfirmed = FALSE;

  /**
   * The original encryption profile.
   *
   * @var \Drupal\encrypt\Entity\EncryptionProfile|null
   *   The original EncryptionProfile entity or NULL if this is a new one.
   */
  protected $originalProfile = NULL;

  /**
   * Constructs a EncryptionProfileForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Encrypt\EncryptService $encrypt_service
   *   The lazy context repository service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EncryptService $encrypt_service) {
    $this->configFactory = $config_factory;
    $this->encryptService = $encrypt_service;
  }

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

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

    // If the form is rebuilding.
    if ($form_state
      ->isRebuilding()) {

      // If an encryption method change triggered the rebuild.
      if ($form_state
        ->getTriggeringElement()['#name'] == 'encryption_method') {

        // Update the encryption method plugin.
        $this
          ->updateEncryptionMethod($form_state);
      }
    }
    elseif ($this->operation == "edit") {

      // Only when the form is first built.

      /* @var $encryption_profile \Drupal\encrypt\Entity\EncryptionProfile */
      $encryption_profile = $this->entity;
      $this->originalProfile = clone $encryption_profile;
    }
    return parent::buildForm($form, $form_state);
  }

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

    /* @var $encryption_profile \Drupal\encrypt\Entity\EncryptionProfile */
    $encryption_profile = $this->entity;

    // If the profile is being edited and editing has not been confirmed yet,
    // display a warning and require confirmation.
    if ($this->operation == "edit" && !$this->editConfirmed) {
      $form['confirm_edit'] = [
        '#type' => 'markup',
        '#markup' => $this
          ->t('Be extremely careful when editing an encryption profile! It may result in making data encrypted with this profile unreadable. Are you sure you want to edit this profile?'),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      ];
      return $form;
    }

    // If editing has been confirmed, display the edit form.
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $encryption_profile
        ->label(),
      '#description' => $this
        ->t("Label for the encryption profile."),
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $encryption_profile
        ->id(),
      '#machine_name' => [
        'exists' => '\\Drupal\\encrypt\\Entity\\EncryptionProfile::load',
      ],
      '#disabled' => !$encryption_profile
        ->isNew(),
    ];

    // This is the element that contains all of the dynamic parts of the form.
    $form['encryption'] = [
      '#type' => 'container',
      '#prefix' => '<div id="encrypt-settings">',
      '#suffix' => '</div>',
    ];
    $encryption_methods = $this->encryptService
      ->loadEncryptionMethods(FALSE);
    $method_options = [];

    // Show the current encryption plugin, even if deprecated.
    if (!$encryption_profile
      ->isNew()) {
      $method = $encryption_profile
        ->getEncryptionMethod();
      $method_options[$method
        ->getPluginId()] = $method
        ->getLabel();
    }
    foreach ($encryption_methods as $plugin_id => $definition) {
      $method_options[$plugin_id] = (string) $definition['title'];
    }
    $form['encryption']['encryption_method'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Encryption Method'),
      '#description' => $this
        ->t('Select the method used for encryption'),
      '#options' => $method_options,
      '#required' => TRUE,
      '#default_value' => $encryption_profile
        ->getEncryptionMethodId(),
      '#ajax' => [
        'callback' => [
          $this,
          'ajaxUpdateSettings',
        ],
        'event' => 'change',
        'wrapper' => 'encrypt-settings',
      ],
    ];
    $form['encryption']['encryption_method_configuration'] = [
      '#type' => 'container',
      '#title' => $this
        ->t('Encryption method settings'),
      '#title_display' => FALSE,
      '#tree' => TRUE,
    ];
    if ($encryption_profile
      ->getEncryptionMethod() instanceof EncryptionMethodPluginFormInterface) {
      $plugin_form_state = $this
        ->createPluginFormState($form_state);
      $form['encryption']['encryption_method_configuration'] += $encryption_profile
        ->getEncryptionMethod()
        ->buildConfigurationForm([], $plugin_form_state);
      $form_state
        ->setValue('encryption_method_configuration', $plugin_form_state
        ->getValues());
    }
    $form['encryption']['encryption_key'] = [
      '#type' => 'key_select',
      '#title' => $this
        ->t('Encryption Key'),
      '#required' => TRUE,
      '#default_value' => $encryption_profile
        ->getEncryptionKeyId(),
    ];

    // Filter the list of available keys by the "encryption" key type group.
    $key_filters = [
      'type_group' => 'encryption',
    ];
    $form['encryption']['encryption_key']['#key_filters'] = $key_filters;
    return $form;
  }

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

    // If the profile is being edited and editing has not been confirmed yet.
    if ($this->operation == "edit" && !$this->editConfirmed) {
      return [
        'submit' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Edit'),
          '#button_type' => 'primary',
          '#submit' => [
            [
              $this,
              'confirmEdit',
            ],
          ],
        ],
        'cancel' => [
          '#type' => 'link',
          '#title' => $this
            ->t('Cancel'),
          '#attributes' => [
            'class' => [
              'button',
            ],
          ],
          '#url' => Url::fromRoute('entity.encryption_profile.collection'),
          '#cache' => [
            'contexts' => [
              'url.query_args:destination',
            ],
          ],
        ],
      ];
    }
    else {
      return parent::actions($form, $form_state);
    }
  }

  /**
   * Creates a FormStateInterface object for a plugin.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state to copy values from.
   *
   * @return \Drupal\Core\Form\FormStateInterface
   *   A clone of the form state object with values from the plugin.
   */
  protected function createPluginFormState(FormStateInterface $form_state) {

    // Clone the form state.
    $plugin_form_state = clone $form_state;

    // Clear the values, except for this plugin type's settings.
    $plugin_form_state
      ->setValues($form_state
      ->getValue('encryption_method_configuration', []));
    return $plugin_form_state;
  }

  /**
   * AJAX callback to update the dynamic settings on the form.
   *
   * @param array $form
   *   The form definition array for the encryption profile form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The element to update in the form.
   */
  public function ajaxUpdateSettings(array &$form, FormStateInterface $form_state) {
    return $form['encryption'];
  }

  /**
   * Update the EncryptionMethod plugin.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  protected function updateEncryptionMethod(FormStateInterface $form_state) {

    /* @var $encryption_profile \Drupal\encrypt\Entity\EncryptionProfile */
    $encryption_profile = $this->entity;

    /* @var $plugin \Drupal\encrypt\EncryptionMethodInterface */
    $plugin = $encryption_profile
      ->getEncryptionMethod();
    $encryption_profile
      ->setEncryptionMethod($plugin);

    // If an original profile exists and the plugin ID matches the existing one.
    if ($this->originalProfile && $this->originalProfile
      ->getEncryptionMethod()
      ->getPluginId() == $plugin
      ->getPluginId()) {

      // Use the configuration from the original profile's plugin.
      $configuration = $this->originalProfile
        ->getEncryptionMethod()
        ->getConfiguration();
    }
    else {

      // Use the plugin's default configuration.
      $configuration = $plugin
        ->defaultConfiguration();
    }
    $plugin
      ->setConfiguration($configuration);
    $form_state
      ->setValue('encryption_method_configuration', []);
    $form_state
      ->getUserInput()['encryption_method_configuration'] = [];
  }

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

    // Only validate when submitting the form, not on AJAX rebuild.
    if (!$form_state
      ->isSubmitted()) {
      return;
    }

    // If the profile is being edited and editing has not been confirmed yet.
    if ($this->operation == "edit" && !$this->editConfirmed) {
      return;
    }

    // If the encryption method contains a config form, validate it as well.
    if ($plugin = $this->entity
      ->getEncryptionMethod()) {
      if ($plugin instanceof EncryptionMethodPluginFormInterface) {
        $plugin_form_state = $this
          ->createPluginFormState($form_state);
        $plugin
          ->validateConfigurationForm($form, $plugin_form_state);
        $form_state
          ->setValue('encryption_method_configuration', $plugin_form_state
          ->getValues());
        $this
          ->moveFormStateErrors($plugin_form_state, $form_state);
        $this
          ->moveFormStateStorage($plugin_form_state, $form_state);
      }
    }
    $form_state
      ->cleanValues();

    /** @var \Drupal\encrypt\Entity\EncryptionConfiguration $entity */
    $this->entity = $this
      ->buildEntity($form, $form_state);

    // Validate the EncryptionProfile entity.
    $errors = $this->entity
      ->validate();
    if ($errors) {
      $form_state
        ->setErrorByName('encryption_key', implode(';', $errors));
    }
  }

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

    // Submit plugin configuration if available.
    if ($plugin = $this->entity
      ->getEncryptionMethod()) {
      if ($plugin instanceof EncryptionMethodPluginFormInterface) {
        $plugin_form_state = $this
          ->createPluginFormState($form_state);
        $plugin
          ->submitConfigurationForm($form, $plugin_form_state);
        $form_state
          ->setValue('encryption_method_configuration', $plugin_form_state
          ->getValues());
      }
    }
    parent::submitForm($form, $form_state);
  }

  /**
   * Submit handler for the edit confirmation button.
   *
   * @param array $form
   *   The form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   */
  public function confirmEdit(array &$form, FormStateInterface $form_state) {
    $this->editConfirmed = TRUE;
    $form_state
      ->setRebuild();
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $encryption_profile = $this->entity;
    $status = $encryption_profile
      ->save();
    if ($status) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Saved the %label encryption profile.', [
        '%label' => $encryption_profile
          ->label(),
      ]));
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('The %label encryption profile was not saved.', [
        '%label' => $encryption_profile
          ->label(),
      ]));
    }
    $form_state
      ->setRedirectUrl($encryption_profile
      ->toUrl('collection'));
  }

  /**
   * Moves form errors from one form state to another.
   *
   * @param \Drupal\Core\Form\FormStateInterface $from
   *   The form state object to move from.
   * @param \Drupal\Core\Form\FormStateInterface $to
   *   The form state object to move to.
   */
  protected function moveFormStateErrors(FormStateInterface $from, FormStateInterface $to) {
    foreach ($from
      ->getErrors() as $name => $error) {
      $to
        ->setErrorByName($name, $error);
    }
  }

  /**
   * Moves storage variables from one form state to another.
   *
   * @param \Drupal\Core\Form\FormStateInterface $from
   *   The form state object to move from.
   * @param \Drupal\Core\Form\FormStateInterface $to
   *   The form state object to move to.
   */
  protected function moveFormStateStorage(FormStateInterface $from, FormStateInterface $to) {
    foreach ($from
      ->getStorage() as $index => $value) {
      $to
        ->set($index, $value);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EncryptionProfileForm::$configFactory protected property The configuration factory. Overrides FormBase::$configFactory
EncryptionProfileForm::$editConfirmed protected property Keeps track of extra confirmation step on profile edit.
EncryptionProfileForm::$encryptService protected property The EncryptService definition.
EncryptionProfileForm::$originalProfile protected property The original encryption profile.
EncryptionProfileForm::actions public function Returns an array of supported actions for the current entity form. Overrides EntityForm::actions
EncryptionProfileForm::ajaxUpdateSettings public function AJAX callback to update the dynamic settings on the form.
EncryptionProfileForm::buildForm public function Form constructor. Overrides EntityForm::buildForm
EncryptionProfileForm::confirmEdit public function Submit handler for the edit confirmation button.
EncryptionProfileForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
EncryptionProfileForm::createPluginFormState protected function Creates a FormStateInterface object for a plugin.
EncryptionProfileForm::form public function Gets the actual form array to be built. Overrides EntityForm::form
EncryptionProfileForm::moveFormStateErrors protected function Moves form errors from one form state to another.
EncryptionProfileForm::moveFormStateStorage protected function Moves storage variables from one form state to another.
EncryptionProfileForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
EncryptionProfileForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides EntityForm::submitForm
EncryptionProfileForm::updateEncryptionMethod protected function Update the EncryptionMethod plugin.
EncryptionProfileForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
EncryptionProfileForm::__construct public function Constructs a EncryptionProfileForm object.
EntityForm::$entity protected property The entity being used by this form. 7
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 2
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties 7
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::__get public function
EntityForm::__set public function
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.