You are here

class SensorListBuilder in Monitoring 8

Defines a class to build a listing of sensor config entities.

Hierarchy

Expanded class hierarchy of SensorListBuilder

See also

\Drupal\monitoring\Entity\SensorConfig

File

src/SensorListBuilder.php, line 22
Contains \Drupal\monitoring\SensorListBuilder.

Namespace

Drupal\monitoring
View 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

Namesort descending Modifiers Type Description Overrides
ConfigEntityListBuilder::getDefaultOperations public function Gets this list's default operations. Overrides EntityListBuilder::getDefaultOperations 15
ConfigEntityListBuilder::load public function Loads entities of this type from storage for listing. Overrides EntityListBuilder::load 7
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
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 2
EntityHandlerBase::moduleHandler protected function Gets the module handler. 2
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
EntityListBuilder::$entityType protected property Information about the entity type.
EntityListBuilder::$entityTypeId protected property The entity type ID.
EntityListBuilder::$storage protected property The entity storage class. 1
EntityListBuilder::buildOperations public function Builds a renderable list of operation links for the entity. 2
EntityListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance 20
EntityListBuilder::ensureDestination protected function Ensures that a destination is present on the given URL.
EntityListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. 4
EntityListBuilder::getLabel Deprecated protected function Gets the label of an entity.
EntityListBuilder::getOperations public function Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface::getOperations 2
EntityListBuilder::getStorage public function Gets the entity storage. Overrides EntityListBuilderInterface::getStorage
EntityListBuilder::getTitle protected function Gets the title of the page. 1
EntityListBuilder::__construct public function Constructs a new EntityListBuilder object. 16
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.
SensorListBuilder::$limit protected property The number of entities to list per page, or FALSE to list all entities. Overrides EntityListBuilder::$limit
SensorListBuilder::buildForm public function Form constructor. Overrides FormInterface::buildForm
SensorListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides EntityListBuilder::buildHeader
SensorListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListBuilder::buildRow
SensorListBuilder::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SensorListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder::render
SensorListBuilder::submitForm public function Form submission handler. Overrides FormInterface::submitForm
SensorListBuilder::validateForm public function Implements \Drupal\Core\Form\FormInterface::validateForm(). Overrides FormInterface::validateForm
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.