class UserFlagWidget in Flag Search API 8
The user_flag widget.
Plugin annotation
@FacetsWidget(
id = "user_flag",
label = @Translation("User Flags"),
description = @Translation("A simple widget that shows a single checkbox of a user's flagged content by Flag Search API"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\facets\Widget\WidgetPluginBase implements WidgetPluginInterface
- class \Drupal\flag_search_api\Plugin\facets\widget\UserFlagWidget implements ContainerFactoryPluginInterface
- class \Drupal\facets\Widget\WidgetPluginBase implements WidgetPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of UserFlagWidget
File
- src/
Plugin/ facets/ widget/ UserFlagWidget.php, line 21
Namespace
Drupal\flag_search_api\Plugin\facets\widgetView source
class UserFlagWidget extends WidgetPluginBase implements ContainerFactoryPluginInterface {
/**
* AccountProxy.
*
* @var \Drupal\Core\Session\AccountProxy
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_user'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'flags_label' => "My Flagged Items",
'no_flags_label' => "No Flagged Items Available",
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet) {
// Get current user ID.
$uid = $this->currentUser
->id();
$userFlagResult = FALSE;
// Get results.
$results = $facet
->getResults();
foreach ($results as $result) {
// Select a result that matches the current user.
if ($result
->getRawValue() == $uid) {
$userFlagResult = $result;
$userFlagResult
->setDisplayValue($this
->getConfiguration()['flags_label']);
}
}
if ($userFlagResult) {
// Replace all results with the selected result.
$facet
->setResults([
$userFlagResult,
]);
}
else {
// Replace all results with an empty result.
$emptyResult = new Result($facet, $uid, $this
->getConfiguration()['no_flags_label'], 0);
$facet
->setResults([
$emptyResult,
]);
}
// Go through normal build process with checkboxes.
$build = parent::build($facet);
$build['#attributes']['class'][] = 'js-facets-checkbox-links';
$build['#attached']['library'][] = 'facets/drupal.facets.checkbox-widget';
return $build;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$form = parent::buildConfigurationForm($form, $form_state, $facet);
$form['flags_label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Flags label'),
'#description' => $this
->t('This text will be used for the flags checkbox label.'),
'#default_value' => $this
->getConfiguration()['flags_label'],
];
$form['no_flags_label'] = [
'#type' => 'textfield',
'#title' => $this
->t('No Flags label'),
'#description' => $this
->t("This text will be used when there aren't any matching flags."),
'#default_value' => $this
->getConfiguration()['no_flags_label'],
];
return $form;
}
}
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 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
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. | |
UserFlagWidget:: |
protected | property | AccountProxy. | |
UserFlagWidget:: |
public | function |
Builds the facet widget for rendering. Overrides WidgetPluginBase:: |
|
UserFlagWidget:: |
public | function |
Provides a configuration form for this widget. Overrides WidgetPluginBase:: |
|
UserFlagWidget:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
UserFlagWidget:: |
public | function |
Gets default configuration for this plugin. Overrides WidgetPluginBase:: |
|
UserFlagWidget:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides WidgetPluginBase:: |
|
WidgetPluginBase:: |
protected | property | The facet the widget is being built for. | |
WidgetPluginBase:: |
protected | property | Show the amount of results next to the result. | |
WidgetPluginBase:: |
protected | function | Builds a renderable array of result items. | 1 |
WidgetPluginBase:: |
protected | function | Builds a facet result item. | |
WidgetPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
WidgetPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
WidgetPluginBase:: |
protected | function | Provides a full array of possible theme functions to try for a given hook. | |
WidgetPluginBase:: |
public | function |
Picks the preferred query type for this widget. Overrides WidgetPluginInterface:: |
3 |
WidgetPluginBase:: |
public | function |
Checks is a specific property is required for this widget. Overrides WidgetPluginInterface:: |
2 |
WidgetPluginBase:: |
protected | function | Returns the text or link for an item. | |
WidgetPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
WidgetPluginBase:: |
public | function |
Checks if the facet is supported by this processor. Overrides WidgetPluginInterface:: |
1 |