class SensorListBuilder in Monitoring 8
Defines a class to build a listing of sensor config entities.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface uses MessengerTrait, RedirectDestinationTrait
- class \Drupal\Core\Config\Entity\ConfigEntityListBuilder
- class \Drupal\monitoring\SensorListBuilder implements FormInterface
- class \Drupal\Core\Config\Entity\ConfigEntityListBuilder
- class \Drupal\Core\Entity\EntityListBuilder implements EntityHandlerInterface, EntityListBuilderInterface uses MessengerTrait, RedirectDestinationTrait
Expanded class hierarchy of SensorListBuilder
See also
\Drupal\monitoring\Entity\SensorConfig
File
- src/
SensorListBuilder.php, line 22 - Contains \Drupal\monitoring\SensorListBuilder.
Namespace
Drupal\monitoringView source
class SensorListBuilder extends ConfigEntityListBuilder implements FormInterface {
/**
* {@inheritdoc}
*/
protected $limit = FALSE;
/**
* {@inheritdoc}
*/
public function buildHeader() {
// Overrides the original Header completely.
$header['category'] = $this
->t('Category');
$header['label'] = $this
->t('Label');
$header['description'] = $this
->t('Description');
$header['operations'] = $this
->t('Operations');
return $header;
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\monitoring\Entity\SensorConfig $entity */
$row['category']['data'] = $entity
->getCategory();
$row['category']['class'][] = 'table-filter-category';
$row['label']['data'] = $entity
->label();
$row['label']['class'][] = 'table-filter-text-source';
$plugin_definition = monitoring_sensor_manager()
->getSensorConfigByName($entity
->id())
->getPlugin()
->getPluginDefinition();
$row['description']['data'] = array(
'#type' => 'details',
'#title' => $entity
->getDescription() ?: $this
->t('Details'),
'#open' => FALSE,
'#tree' => TRUE,
'#attributes' => array(
'class' => array(
'monitoring-sensor-details',
),
),
);
$row['description']['data'][$entity
->id()]['id'] = [
'#type' => 'item',
'#title' => $this
->t('Sensor ID'),
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
'#markup' => '<span class="table-filter-text-source">' . $entity
->id() . '</span>',
];
$row['description']['data'][$entity
->id()]['sensor_type'] = [
'#type' => 'item',
'#title' => $this
->t('Sensor type'),
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
'#markup' => '<span class="table-filter-sensor-type">' . $plugin_definition['label'] . '</span>',
];
$url = Url::fromRoute('entity.monitoring_sensor_config.details_form', array(
'monitoring_sensor_config' => $entity
->id(),
));
$row = $row + parent::buildRow($entity);
// Adds the link to details page if sensor is enabled.
/** @var \Drupal\monitoring\Entity\SensorConfig $sensor_config */
$sensor_config = SensorConfig::load($entity
->id());
if ($sensor_config
->isEnabled()) {
$row['operations']['data']['#links']['details'] = array(
'title' => 'Details',
'url' => $url,
);
}
return $row;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'sensor_overview_form';
}
/**
* Implements \Drupal\Core\Form\FormInterface::validateForm().
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// No validation.
}
/**
* {@inheritdoc}
*/
public function render() {
$form = \Drupal::formBuilder()
->getForm($this);
$form['sensors']['#attributes']['class'][] = 'monitoring-sensor-overview';
$form['#attached']['library'][] = 'monitoring/monitoring.sensor.overview';
$form['#attached']['library'][] = 'monitoring/monitoring';
$form['#attached']['library'][] = 'core/drupal.ajax';
return $form;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$categories = [];
$options = [];
$default_value = [];
$sensor_types = [];
/** @var \Drupal\monitoring\Entity\SensorConfig $entity */
foreach ($this
->load() as $entity) {
$row = $this
->buildRow($entity);
$options[$entity
->id()] = $row;
$default_value[$entity
->id()] = $entity
->isEnabled();
// Get all sensor categories.
if (!isset($categories[$entity
->getCategory()])) {
$categories[$entity
->getCategory()] = $entity
->getCategory();
}
$plugin_definition = monitoring_sensor_manager()
->getSensorConfigByName($entity
->id())
->getPlugin()
->getPluginDefinition();
// Get all sensor plugin types.
$plugin_label_key = $plugin_definition['label']
->render();
if (!isset($sensor_types[$plugin_label_key])) {
$sensor_types[$plugin_label_key] = $plugin_definition['label'];
}
}
asort($sensor_types);
$form['filters'] = array(
'#type' => 'fieldset',
'#attributes' => array(
'class' => array(
'table-filter',
'js-show',
'form--inline',
),
),
'#weight' => -10,
'#title' => $this
->t('Filter'),
);
$form['filters']['sensor_type'] = array(
'#type' => 'select',
'#empty_option' => $this
->t('- All -'),
'#title' => $this
->t('Sensor type'),
'#options' => $sensor_types,
'#attributes' => array(
'class' => array(
'table-filter-select-sensor-type',
),
),
);
$form['filters']['category'] = array(
'#type' => 'select',
'#empty_option' => $this
->t('- All -'),
'#title' => $this
->t('Category'),
'#options' => $categories,
'#attributes' => array(
'class' => array(
'table-filter-select-category',
),
),
);
$form['filters']['text'] = array(
'#type' => 'search',
'#title' => $this
->t('Sensor label or sensor id'),
'#size' => 40,
'#placeholder' => $this
->t('Enter a sensor label or sensor id'),
'#attributes' => array(
'class' => array(
'table-filter-text',
),
'data-table' => '.monitoring-sensor-overview',
'autocomplete' => 'off',
'title' => $this
->t('Enter a part of the sensor label or sensor id to filter by.'),
),
);
$form['sensors'] = array(
'#type' => 'tableselect',
'#header' => $this
->buildHeader(),
'#options' => $options,
'#default_value' => $default_value,
'#attributes' => array(
'id' => 'monitoring-sensors-config-overview',
),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Update enabled sensors'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state
->getValue('sensors') as $sensor_id => $enabled) {
/** @var \Drupal\monitoring\Entity\SensorConfig $sensor */
$sensor = SensorConfig::load($sensor_id);
if ($enabled) {
$sensor->status = TRUE;
}
else {
$sensor->status = FALSE;
}
$sensor
->save();
}
$this
->messenger()
->addMessage($this
->t('Configuration has been saved.'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigEntityListBuilder:: |
public | function |
Gets this list's default operations. Overrides EntityListBuilder:: |
15 |
ConfigEntityListBuilder:: |
public | function |
Loads entities of this type from storage for listing. Overrides EntityListBuilder:: |
7 |
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 | |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 2 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 2 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
EntityListBuilder:: |
protected | property | Information about the entity type. | |
EntityListBuilder:: |
protected | property | The entity type ID. | |
EntityListBuilder:: |
protected | property | The entity storage class. | 1 |
EntityListBuilder:: |
public | function | Builds a renderable list of operation links for the entity. | 2 |
EntityListBuilder:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface:: |
20 |
EntityListBuilder:: |
protected | function | Ensures that a destination is present on the given URL. | |
EntityListBuilder:: |
protected | function | Loads entity IDs using a pager sorted by the entity id. | 4 |
EntityListBuilder:: |
protected | function | Gets the label of an entity. | |
EntityListBuilder:: |
public | function |
Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface:: |
2 |
EntityListBuilder:: |
public | function |
Gets the entity storage. Overrides EntityListBuilderInterface:: |
|
EntityListBuilder:: |
protected | function | Gets the title of the page. | 1 |
EntityListBuilder:: |
public | function | Constructs a new EntityListBuilder object. | 16 |
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. | |
SensorListBuilder:: |
protected | property |
The number of entities to list per page, or FALSE to list all entities. Overrides EntityListBuilder:: |
|
SensorListBuilder:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
SensorListBuilder:: |
public | function |
Builds the header row for the entity listing. Overrides EntityListBuilder:: |
|
SensorListBuilder:: |
public | function |
Builds a row for an entity in the entity listing. Overrides EntityListBuilder:: |
|
SensorListBuilder:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SensorListBuilder:: |
public | function |
Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder:: |
|
SensorListBuilder:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
SensorListBuilder:: |
public | function |
Implements \Drupal\Core\Form\FormInterface::validateForm(). Overrides FormInterface:: |
|
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. |