You are here

class EmergencyForm in Display Suite 8.3

Same name and namespace in other branches
  1. 8.4 src/Form/EmergencyForm.php \Drupal\ds\Form\EmergencyForm
  2. 8.2 src/Form/EmergencyForm.php \Drupal\ds\Form\EmergencyForm

Emergency form for DS.

Hierarchy

Expanded class hierarchy of EmergencyForm

1 string reference to 'EmergencyForm'
ds.routing.yml in ./ds.routing.yml
ds.routing.yml

File

src/Form/EmergencyForm.php, line 15

Namespace

Drupal\ds\Form
View source
class EmergencyForm extends ConfigFormBase {

  /**
   * State object.
   *
   * @var \Drupal\Core\State\State
   */
  protected $state;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a \Drupal\ds\Form\EmergencyForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The config factory.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key value store.
   */
  public function __construct(ConfigFactory $config_factory, ModuleHandlerInterface $module_handler, StateInterface $state) {
    parent::__construct($config_factory);
    $this->moduleHandler = $module_handler;
    $this->state = $state;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['ds_fields_error'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Fields error'),
    ];
    $form['ds_fields_error']['disable'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('In case you get an error after configuring a layout printing a message like "Fatal error: Unsupported operand types", you can temporarily disable adding fields from DS. You probably are trying to render an node inside a node, for instance through a view, which is simply not possible. See <a href="http://drupal.org/node/1264386">http://drupal.org/node/1264386</a>.'),
    ];
    $form['ds_fields_error']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->state
        ->get('ds.disabled', FALSE) ? $this
        ->t('Enable attaching fields') : $this
        ->t('Disable attaching fields'),
      '#submit' => [
        '::submitFieldAttach',
      ],
      '#weight' => 1,
    ];
    if ($this->moduleHandler
      ->moduleExists('ds_extras')) {
      $region_blocks = $this
        ->config('ds_extras.settings')
        ->get('region_blocks');
      if (!empty($region_blocks)) {
        $region_blocks_options = [];
        foreach ($region_blocks as $key => $info) {
          $region_blocks_options[$key] = $info['title'];
        }
        $form['region_to_block'] = [
          '#type' => 'fieldset',
          '#title' => $this
            ->t('Block regions'),
        ];
        $form['region_to_block']['remove_block_region'] = [
          '#type' => 'checkboxes',
          '#options' => $region_blocks_options,
          '#description' => $this
            ->t('In case you renamed a content type, you will not see the configured block regions anymore, however the block on the block settings page is still available. On this screen you can remove orphaned block regions.'),
        ];
        $form['region_to_block']['submit'] = [
          '#type' => 'submit',
          '#value' => $this
            ->t('Remove block regions'),
          '#submit' => [
            '::submitRegionToBlock',
          ],
          '#weight' => 1,
        ];
      }
    }
    return $form;
  }

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

    // empty.
  }

  /**
   * Submit callback for the fields error form.
   */
  public function submitFieldAttach(array &$form, FormStateInterface $form_state) {
    $this->state
      ->set('ds.disabled', $this->state
      ->get('ds.disabled', FALSE) ? FALSE : TRUE);
    $this
      ->messenger()
      ->addMessage(t('The configuration options have been saved.'));
  }

  /**
   * Submit callback for the region to block form.
   */
  public function submitRegionToBlock(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('remove_block_region')) {
      $save = FALSE;
      $region_blocks = $this
        ->config('ds_extras.settings')
        ->get('region_blocks');
      $remove = $form_state
        ->getValue('remove_block_region');
      foreach ($remove as $key => $value) {
        if ($value !== 0 && $key == $value) {
          $save = TRUE;

          // Make sure there is no active block instance for this ds block
          // region.
          if (\Drupal::moduleHandler()
            ->moduleExists('block')) {
            $ids = \Drupal::entityQuery('block')
              ->condition('plugin', 'ds_region_block:' . $key)
              ->execute();

            /* @var \Drupal\block\BlockInterface $block_storage */
            $block_storage = \Drupal::service('entity_type.manager')
              ->getStorage('block');
            foreach ($block_storage
              ->loadMultiple($ids) as $block) {
              $block
                ->delete();
            }
          }
          unset($region_blocks[$key]);
        }
      }
      if ($save) {
        $this
          ->messenger()
          ->addMessage($this
          ->t('Block regions were removed.'));

        // Clear cached block and ds plugin definitions.
        \Drupal::service('plugin.manager.block')
          ->clearCachedDefinitions();
        \Drupal::service('plugin.manager.ds')
          ->clearCachedDefinitions();
        $this
          ->config('ds_extras.settings')
          ->set('region_blocks', $region_blocks)
          ->save();
      }
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('No block regions were removed.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'ds_extras.settings',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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
EmergencyForm::$moduleHandler protected property The module handler.
EmergencyForm::$state protected property State object.
EmergencyForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
EmergencyForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
EmergencyForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
EmergencyForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EmergencyForm::submitFieldAttach public function Submit callback for the fields error form.
EmergencyForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
EmergencyForm::submitRegionToBlock public function Submit callback for the region to block form.
EmergencyForm::__construct public function Constructs a \Drupal\ds\Form\EmergencyForm object. Overrides ConfigFormBase::__construct
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.