class KanbanFilterForm in Content Planner 8
KanbanFilterForm class.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\content_kanban\Form\KanbanFilterForm
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\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
KanbanFilterForm:: |
protected | property | The entity type manager. | |
KanbanFilterForm:: |
protected | property | An array with the form params. | |
KanbanFilterForm:: |
protected | property | The Kanban service. | |
KanbanFilterForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
KanbanFilterForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
KanbanFilterForm:: |
public static | function | Gets the content type filter from request. | |
KanbanFilterForm:: |
public | function | Gets the Date Range options. | |
KanbanFilterForm:: |
public static | function | Gets the State Date Range from request. | |
KanbanFilterForm:: |
public static | function | Gets the Date Range options. | |
KanbanFilterForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
KanbanFilterForm:: |
public static | function | Gets the State filter from request. | |
KanbanFilterForm:: |
protected | function | Gets the State options. | |
KanbanFilterForm:: |
public static | function | Gets the User ID filter from request. | |
KanbanFilterForm:: |
protected | function | Gets the User options. | |
KanbanFilterForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
KanbanFilterForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
KanbanFilterForm:: |
public | function | ||
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |