Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / src / Form / CustomFormItemDeleteForm.php
Size: Mime:
<?php

namespace Drupal\custom_forms\Form;

use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\custom_forms\CustomFormItem;
use Drupal\custom_forms\CustomFormItemFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class CustomFormItemDeleteForm
 *
 * @package Drupal\custom_forms\Form
 */
class CustomFormItemDeleteForm extends FormBase {

  /**
   * The factory in charge of CRUD operations for custom form items.
   *
   * @var \Drupal\custom_forms\CustomFormItemFactory
   */
  protected $itemFactory;

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

  /**
   * {@inheritdoc}
   */
  public function __construct(CustomFormItemFactory $item_factory) {
    $this->itemFactory = $item_factory;
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $attributes     = \Drupal::request()->attributes;
    $custom_form    = $attributes->get('custom_form');
    $item           = $this->itemFactory->loadItem($attributes->get('item'));

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

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

    $message = $this->t('Are you sure you want to delete %item-label?',
      [
        '%item-label' => $item->getSetting('label'),
      ]
    );


    $form['message'] = [
      '#type' => 'item',
      '#description' => $message
    ];

    $form['actions'] = [
      '#type' => 'actions',
    ];

    $form['actions']['delete'] = [
      '#type' => 'submit',
      '#value' => $this->t('Delete'),
      '#attributes' => [
        'class' => [
          'button',
          'button-action',
          'button--primary',
        ]
      ],
    ];

    $form['actions']['cancel'] = [
      '#type' => 'link',
      '#title' => $this->t('Cancel'),
      '#url' => Url::fromRoute('entity.custom_form.fields_form', ['custom_form' => $custom_form->id()]),
      '#attributes' => [
        'class' => [
          'button',
          'button-action',
          'button--secondary',
        ],
      ],
      '#limit_validation_errors' => [],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->cleanValues()->getValues();

    /** @var \Drupal\custom_forms\Entity\CustomForm $custom_form */
    $custom_form = $values['custom_form'];

    /** @var \Drupal\custom_forms\CustomFormItem $previous_item */
    $previous_item = $values['item'];

    // Storing the label for use after deletion.
    $label = $previous_item->getSetting('label');

    // Create a new custom form revision since we are modifying it by removing a field.
    $revision_saved = FALSE;
    $custom_form->setNewRevision(TRUE);
    $custom_form->setRevisionTranslationAffected(TRUE); // for some reason required for log messages to show up?
    $custom_form->setRevisionLogMessage(
      $this->t('Deleted field: @field-label', [
        '@field-label' => $label
      ]));
    $custom_form->setRevisionCreationTime(\Drupal::time()->getRequestTime());
    $custom_form->setRevisionUserId(\Drupal::currentUser()->id());
    try {
      $custom_form->save();
      $revision_saved = TRUE;
    } catch (EntityStorageException $e) {
      $error_message = $this->t('Failed during creation of new revision. Message = %message', [
        '%message' => $e->getMessage()
      ]);
      \Drupal::logger('Custom Forms')->error($error_message);
      $this->messenger()->addError($this->t('Failed during creation of new revision, please try again. If it continues please contact an administrator.'));
    }

    if ($revision_saved) {
      $item = $this->itemFactory->loadItem($previous_item->id(), NULL, $custom_form->getLoadedRevisionId(), $previous_item->getLangcode());

      if (!empty($item)) {
        $item->delete();
        $this->messenger()->addMessage($this->t('%item-label was successfully deleted.', ['%item-label' => $label]));
      }
    }
    $form_state->setRedirect('entity.custom_form.fields_form', ['custom_form' => $custom_form->id()]);
  }
}