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 / Plugin / Block / CustomFormBlock.php
Size: Mime:
<?php


namespace Drupal\custom_forms\Plugin\Block;


use Drupal\Core\Annotation\Translation;
use Drupal\Core\Block\Annotation\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\custom_forms\Controller\CustomFormQueryArgsController;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class CustomFormBlock
 *
 * @package Drupal\custom_forms\Plugin\Block
 *
 * @Block(
 *   id = "custom_form_block",
 *   admin_label = @Translation("Custom Form"),
 *   category = @Translation("Custom Forms"),
 * )
 */
class CustomFormBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * @var \Drupal\Core\Entity\EntityStorageInterface
   *   The storage for custom forms.
   */
  protected $customFormStorage;

  /**
   * @var \Drupal\Core\Entity\EntityStorageInterface
   *   The storage for custom form types.
   */
  protected $customFormTypeStorage;

  /**
   * @var \Drupal\Core\Language\LanguageManagerInterface
   *   The language manager service.
   */
  protected $languageManager;

  /**
   * @var \Drupal\custom_forms\Controller\CustomFormQueryArgsController
   *   The service for loading the query arguments.
   */
  protected $queryArgs;

  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.manager'),
      $container->get('language_manager'),
      $container->get('custom_form.query_arguments')
    );
  }

  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    EntityTypeManagerInterface $entity_type_manager,
    LanguageManagerInterface $language_manager,
    CustomFormQueryArgsController $query_args
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->customFormStorage = $entity_type_manager->getStorage('custom_form');
    $this->customFormTypeStorage = $entity_type_manager->getStorage('custom_form_type');
    $this->languageManager = $language_manager;
    $this->queryArgs = $query_args;
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];
    $config = $this->getConfiguration();

    $cache_contexts = $this->getCacheContexts();
    $cache_tags     = $this->getCacheTags();
    $cache_max_age  = $this->getCacheMaxAge();
    // Only add content if a custom form was specified.
    if (!empty($config['custom_form'])) {
      // We want to load the custom form using lazy builder to improve
      // page caching.
      $build['custom_form'] = [
        '#lazy_builder' => ['custom_form.lazy_builder:generateCustomForm', ['custom_form' => $config['custom_form']]],
        '#create_placeholder' => TRUE,
      ];

      // Add content language cache context.
      $cache_contexts[] = 'languages:language_content';

      // Add URL cache context for query parameters.
      $args = $this->queryArgs->getQueryArgs($config['custom_form']);
      foreach ($args as $argument) {
        $cache_contexts[] = 'url.query_args:'.$argument;
      }

      $cache_tags[] = 'custom_form:'.$config['custom_form'];
    }

    $build['#cache']['contexts'] = $cache_contexts;
    $build['#cache']['tags'] = $cache_tags;
    $build['#cache']['max-age'] = $cache_max_age;

    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);
    $config = $this->getConfiguration();

    $current_language = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);

    /** @var \Drupal\custom_forms\CustomFormInterface[] $custom_forms */
    $custom_forms = $this->customFormStorage->loadByProperties([
      'status' => 1,
      'langcode' => $current_language->getId()
    ]);

    $options = [];
    foreach ($custom_forms as $custom_form) {
      $bundle = $this->customFormTypeStorage->load($custom_form->bundle());
      $options[$bundle->label()][$custom_form->id()] = $custom_form->label();
    }

    $form['custom_form'] = [
      '#type' => 'select',
      '#title' => $this->t('Custom form'),
      '#description' => $this->t('Select the custom form to embed in this block.'),
      '#required' => TRUE,
      '#empty_option' => '- ' . $this->t('Select custom form') . ' -',
      '#options' => $options,
      '#default_value' => $config['custom_form'] ?? NULL,
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    parent::blockSubmit($form, $form_state);
    $values = $form_state->cleanValues()->getValues();
    $this->configuration['custom_form'] = $values['custom_form'];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $configuration = parent::defaultConfiguration();

    $configuration['label_display'] = 'hidden';

    return $configuration;
  }

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

    // Hide the description
    $form['admin_label']['#type'] = 'hidden';

    // Hide the label field.
    $form['label']['#type'] = 'value';
    $form['label_display']['#type'] = 'value';

    return $form;
  }

}