You are here

class KanbanFilterForm in Content Planner 8

KanbanFilterForm class.

Hierarchy

Expanded class hierarchy of KanbanFilterForm

2 files declare their use of KanbanFilterForm
Kanban.php in modules/content_kanban/src/Component/Kanban.php
KanbanService.php in modules/content_kanban/src/KanbanService.php

File

modules/content_kanban/src/Form/KanbanFilterForm.php, line 19

Namespace

Drupal\content_kanban\Form
View source
class KanbanFilterForm extends FormBase {

  /**
   * The Kanban service.
   *
   * @var \Drupal\content_kanban\KanbanService
   */
  protected $kanbanService;

  /**
   * An array with the form params.
   *
   * @var array
   */
  protected $formParams = [];

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(KanbanService $kanban_service, EntityTypeManager $entityTypeManager) {
    $this->kanbanService = $kanban_service;
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('content_kanban.kanban_service'), $container
      ->get('entity_type.manager'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $params = []) {
    $this->formParams = $params;
    $form_state
      ->setMethod('GET');
    $form['filters'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Filters'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
      '#attributes' => [
        'class' => [
          'form--inline',
        ],
      ],
    ];

    // User ID.
    $form['filters']['filter_uid'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('User'),
      '#options' => $this
        ->getUserOptions(),
      '#required' => FALSE,
      '#empty_value' => '',
      '#empty_option' => $this
        ->t('- Select -'),
      '#default_value' => self::getUserIdFilter(),
    ];

    // User ID.
    $form['filters']['filter_state'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('States'),
      '#options' => $this
        ->getStateOptions(),
      '#required' => FALSE,
      '#empty_value' => '',
      '#empty_option' => $this
        ->t('All'),
      '#default_value' => self::getStateFilter(),
    ];
    $form['filters']['filter_date_range'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Date range'),
      '#options' => self::getDateRangeOptions(),
      '#required' => FALSE,
      '#empty_value' => '',
      '#empty_option' => $this
        ->t('All'),
      '#default_value' => self::getDateRangeFilter(),
    ];
    $form['filters']['filter_content_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Content type'),
      '#options' => self::getContentTypeOptions($params['workflow_id']),
      '#required' => FALSE,
      '#empty_value' => '',
      '#empty_option' => $this
        ->t('All'),
      '#default_value' => self::getContentTypeFilter(),
      '#required' => FALSE,
    ];
    $form['filters']['search'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Instant Search'),
      '#required' => FALSE,
    ];

    // Actions.
    $form['filters']['actions'] = [
      '#type' => 'actions',
    ];

    // Submit button.
    $form['filters']['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Search'),
    ];
    $form['filters']['actions']['reset'] = [
      '#markup' => Link::createFromRoute($this
        ->t('Reset'), 'content_kanban.kanban')
        ->toString(),
    ];
    return $form;
  }

  /**
   * Gets the User options.
   *
   * @return array
   *   Returns an array with the user options if any or an empty array
   *   otherwise.
   */
  protected function getUserOptions() {
    $options = [];

    // Load Content Moderation entities.
    $content_moderation_entities = $this->kanbanService
      ->getEntityContentModerationEntities($this->formParams['workflow_id']);
    foreach ($content_moderation_entities as $content_moderation_entity) {

      // Get the entity id and entity type id.
      $entityId = $content_moderation_entity->content_entity_id->value;
      $entityTypeId = $content_moderation_entity->content_entity_type_id->value;

      // Get the entity keys and the entity loaded.
      try {
        $entityType = $this->entityTypeManager
          ->getStorage($entityTypeId);
        $entityKeyUserId = $entityType
          ->getEntityType()
          ->getKey('uid');
        if ($entity = $entityType
          ->load($entityId)) {
          $userId = $entity->{$entityKeyUserId}
            ->getValue();
          if ($user_id = $userId[0]['target_id']) {
            if (!array_key_exists($user_id, $options)) {

              // Load user if existing.
              if ($user = $this->entityTypeManager
                ->getStorage('user')
                ->load($user_id)) {

                // Add to options.
                $options[$user_id] = $user->name->value;
              }
            }
          }
        }
      } catch (InvalidPluginDefinitionException $e) {
        watchdog_exception('content_kanban', $e);
      } catch (PluginNotFoundException $e) {
        watchdog_exception('content_kanban', $e);
      }
    }
    return $options;
  }

  /**
   * Gets the State options.
   *
   * @return array
   *   Returns an array with the state options.
   */
  protected function getStateOptions() {
    $options = [];
    foreach ($this->formParams['states'] as $state_id => $state) {
      $options[$state_id] = $state['label'];
    }
    return $options;
  }

  /**
   * Gets the Date Range options.
   *
   * @return array
   *   Returns an array with the date range options.
   */
  public static function getDateRangeOptions() {

    //static for now, maybe it needs to be more dynamic later
    $options = [];

    //@todo t() is bad - how can we get the translation service (without using global context)
    $options['1'] = t('1 Day');
    $options['7'] = t('7 Days');
    $options['30'] = t('30 Days');
    $options['90'] = t('90 Days');
    $options['365'] = t('Year');
    return $options;
  }

  /**
   * Gets the Date Range options.
   *
   * @return array
   *   Returns an array with the date range options.
   */
  public function getContentTypeOptions($workflow_id) {
    $options = [];
    $workflow = $this->entityTypeManager
      ->getStorage('workflow')
      ->load($workflow_id);
    $types = $workflow
      ->get('type_settings')['entity_types']['node'];
    $all_content_types = NodeType::loadMultiple();
    foreach ($types as $key) {
      $options[$key] = $all_content_types[$key]
        ->label();
    }
    return $options;
  }

  /**
   * Gets the User ID filter from request.
   *
   * @return int|null
   *   Returns the filter_uid value if it exists, NULL otherwise.
   */
  public static function getUserIdFilter() {
    if (\Drupal::request()->query
      ->has('filter_uid')) {
      return \Drupal::request()->query
        ->getInt('filter_uid');
    }
    return NULL;
  }

  /**
   * Gets the State filter from request.
   *
   * @return int|null
   *   Returns the filter_state value if it exists, NULL otherwise.
   */
  public static function getStateFilter() {
    if (\Drupal::request()->query
      ->has('filter_state')) {
      return \Drupal::request()->query
        ->get('filter_state');
    }
    return NULL;
  }

  /**
   * Gets the State Date Range from request.
   *
   * @return int|null
   *   Returns the filter_date_range value if it exists, NULL otherwise.
   */
  public static function getDateRangeFilter() {
    if (\Drupal::request()->query
      ->has('filter_date_range')) {
      return \Drupal::request()->query
        ->get('filter_date_range');
    }
    else {
      $config = \Drupal::config(SettingsForm::CONFIG_NAME);
      $date_range = $config
        ->get('default_filter_date_range');
      if ($date_range) {
        return $date_range;
      }
    }
    return SettingsForm::DEFAULT_DATE_RANGE_VALUE;
  }

  /**
   * Gets the content type filter from request.
   *
   * @return string|null
   *   Returns the filter_content_type value if it exists, NULL otherwise.
   */
  public static function getContentTypeFilter() {
    if (\Drupal::request()->query
      ->has('filter_content_type')) {
      return \Drupal::request()->query
        ->get('filter_content_type');
    }
    return NULL;
  }

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

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

}

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
FormBase::$configFactory protected property The config factory. 1
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.
KanbanFilterForm::$entityTypeManager protected property The entity type manager.
KanbanFilterForm::$formParams protected property An array with the form params.
KanbanFilterForm::$kanbanService protected property The Kanban service.
KanbanFilterForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
KanbanFilterForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
KanbanFilterForm::getContentTypeFilter public static function Gets the content type filter from request.
KanbanFilterForm::getContentTypeOptions public function Gets the Date Range options.
KanbanFilterForm::getDateRangeFilter public static function Gets the State Date Range from request.
KanbanFilterForm::getDateRangeOptions public static function Gets the Date Range options.
KanbanFilterForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
KanbanFilterForm::getStateFilter public static function Gets the State filter from request.
KanbanFilterForm::getStateOptions protected function Gets the State options.
KanbanFilterForm::getUserIdFilter public static function Gets the User ID filter from request.
KanbanFilterForm::getUserOptions protected function Gets the User options.
KanbanFilterForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
KanbanFilterForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
KanbanFilterForm::__construct public function
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.