View source
<?php
namespace Drupal\content_kanban\Form;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\content_kanban\KanbanService;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\content_kanban\Form\SettingsForm;
use Drupal\node\Entity\NodeType;
class KanbanFilterForm extends FormBase {
protected $kanbanService;
protected $formParams = [];
protected $entityTypeManager;
public function __construct(KanbanService $kanban_service, EntityTypeManager $entityTypeManager) {
$this->kanbanService = $kanban_service;
$this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('content_kanban.kanban_service'), $container
->get('entity_type.manager'));
}
public function getFormId() {
return 'content_kanban_filter_form';
}
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',
],
],
];
$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(),
];
$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,
];
$form['filters']['actions'] = [
'#type' => 'actions',
];
$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;
}
protected function getUserOptions() {
$options = [];
$content_moderation_entities = $this->kanbanService
->getEntityContentModerationEntities($this->formParams['workflow_id']);
foreach ($content_moderation_entities as $content_moderation_entity) {
$entityId = $content_moderation_entity->content_entity_id->value;
$entityTypeId = $content_moderation_entity->content_entity_type_id->value;
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)) {
if ($user = $this->entityTypeManager
->getStorage('user')
->load($user_id)) {
$options[$user_id] = $user->name->value;
}
}
}
}
} catch (InvalidPluginDefinitionException $e) {
watchdog_exception('content_kanban', $e);
} catch (PluginNotFoundException $e) {
watchdog_exception('content_kanban', $e);
}
}
return $options;
}
protected function getStateOptions() {
$options = [];
foreach ($this->formParams['states'] as $state_id => $state) {
$options[$state_id] = $state['label'];
}
return $options;
}
public static function getDateRangeOptions() {
$options = [];
$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;
}
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;
}
public static function getUserIdFilter() {
if (\Drupal::request()->query
->has('filter_uid')) {
return \Drupal::request()->query
->getInt('filter_uid');
}
return NULL;
}
public static function getStateFilter() {
if (\Drupal::request()->query
->has('filter_state')) {
return \Drupal::request()->query
->get('filter_state');
}
return NULL;
}
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;
}
public static function getContentTypeFilter() {
if (\Drupal::request()->query
->has('filter_content_type')) {
return \Drupal::request()->query
->get('filter_content_type');
}
return NULL;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}