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 / Controller / CustomFormQueryArgsController.php
Size: Mime:
<?php


namespace Drupal\custom_forms\Controller;


use Drupal\Core\Cache\Cache;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\custom_forms\CustomFormItemFactory;
use Drupal\custom_forms\Entity\CustomForm;
use Symfony\Component\DependencyInjection\ContainerInterface;

class CustomFormQueryArgsController extends ControllerBase {

  /**
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   *   The logging channel.
   */
  protected $logger;

  protected $itemFactory;

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

  /**
   * SendGridTemplateService constructor.
   *
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   The logger factory service used to get the logger channel.
   * @param \Drupal\custom_forms\CustomFormItemFactory $item_factory
   *   The custom form item factory used for loading form items.
   */
  public function __construct(
    LoggerChannelFactoryInterface $logger_factory,
    CustomFormItemFactory $item_factory
  ) {
    $this->loggerFactory = $logger_factory;
    $this->logger = $this->loggerFactory->get('Custom Forms');
    $this->itemFactory = $item_factory;
  }

  /**
   * Get an array of all query arguments that are possible to use on a form.
   *
   * @param int $form_id
   *   The id of the custom form to get the query parameters for.
   *
   * @return array
   *   Returns an array of possible query arguments.
   */
  public function getQueryArgs($form_id) : array {
    $cache_id   = 'custom_form:' . $form_id . ':query_args';
    $cache_data = $this->cache('data')->get($cache_id);

    if ($cache_data) {
      $args = $cache_data->data;
    }
    else {
      $args = $this->cacheQueryArgs($form_id);
    }

    return $args;
  }

  /**
   * Cache the query arguments for the custom form.
   *
   * @param int $form_id
   *   The id of the custom form to cache the query arguments for.
   *
   * @return array
   *   Returns an array of the cached query arguments.
   */
  private function cacheQueryArgs($form_id) : array {
    $cache_id               = 'custom_form:' . $form_id . ':query_args';
    $cache_data             = $this->getNewQueryArgs($form_id);

    $this->cache('data')->set($cache_id, $cache_data, Cache::PERMANENT);
    return $cache_data;
  }

  /**
   * Get new query arguments for the custom form.
   *
   * @param int $form_id
   *   The id of the custom form to get the query arguments for.
   *
   * @return array
   *   Returns an array of the query arguments possible on the custom form.
   */
  private function getNewQueryArgs($form_id) : array {
    /** @var \Drupal\custom_forms\CustomFormInterface $custom_form */
    $custom_form = CustomForm::load($form_id);

    if ($custom_form === NULL) {
      $this->logger->error('Failed to load custom form with id @id when attempting to load new query arguments.', ['@id' => $form_id]);
      return [];
    }

    /** @var \Drupal\custom_forms\CustomFormItem[] $items */
    $items = $this->itemFactory->getFormItems($custom_form);

    $args = [];
    foreach ($items as $item) {
      // We only want fields that don't have hidden machine names.
      // This is to avoid adding submit buttons to potential query parameters.
      if ($item->getType() === 'field' && !$item->isMachineNameHidden()) {
        $args[$item->getMachineName()] = $item->getMachineName();
      }
    }

    return $args;
  }

}