You are here

public function NotificationsWidgetSettingsForm::buildForm in Notifications widget 8

Build form.

Overrides ConfigFormBase::buildForm

File

src/Form/NotificationsWidgetSettingsForm.php, line 80

Class

NotificationsWidgetSettingsForm
Provides settings for Notification widget module.

Namespace

Drupal\notifications_widget\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('notifications_widget.settings');
  $commentExists = $this->moduleHandler
    ->moduleExists('comment');

  // User entity.
  $form['user_entity_type'] = [
    '#type' => 'fieldset',
    '#title' => 'Users',
  ];
  $form['user_entity_type'] += $this
    ->prepareEntityTypeForm('user', 'User', $config, 'Entity');

  // Content Type list.
  $nodeTypeStorage = $this->entityTypeManager
    ->getStorage('node_type');
  $nodeTypes = $nodeTypeStorage
    ->loadMultiple();
  $form['content_type'] = [
    '#type' => 'fieldset',
    '#title' => 'Content Types',
  ];
  foreach ($nodeTypes as $nodeType => $nodeTypeData) {
    $nodeTypeName = $nodeTypeData
      ->get('name');
    $form['content_type'] += $this
      ->prepareEntityTypeForm($nodeType, $nodeTypeName, $config, 'Content');
  }

  // Comment Type list.
  if ($commentExists) {
    $commentTypeStorage = $this->entityTypeManager
      ->getStorage('comment_type');
    $commentTypes = $commentTypeStorage
      ->loadMultiple();
    $form['comment_type'] = [
      '#type' => 'fieldset',
      '#title' => 'Comment Types',
    ];
    foreach ($commentTypes as $commentType => $commentTypeData) {
      $commentTypeName = str_replace('Default', '', $commentTypeData
        ->get('label'));
      $form['comment_type'] += $this
        ->prepareEntityTypeForm($commentType, $commentTypeName, $config, 'Comment');
    }
  }

  // Vocabulary list.
  $termStorage = $this->entityTypeManager
    ->getStorage('taxonomy_vocabulary');
  $termTypes = $termStorage
    ->loadMultiple();
  $form['term_type'] = [
    '#type' => 'fieldset',
    '#title' => 'Taxonomy Types',
  ];
  foreach ($termTypes as $termType => $termTypeData) {
    $termTypeName = $termTypeData
      ->get('name');
    $form['term_type'] += $this
      ->prepareEntityTypeForm($termType, $termTypeName, $config, 'Taxonomy');
  }

  /*
   * Allow notification for additional entity types.
   */
  if (!empty($config
    ->get('additional_entity_type'))) {
    $form['additional_entity_type'] = [
      '#type' => 'fieldset',
      '#title' => 'Additional Entity types',
    ];
    $flag = 0;
    $addtionalEntityTypes = explode(',', $config
      ->get('additional_entity_type'));
    foreach ($addtionalEntityTypes as $entityType) {
      if ($this->entityTypeManager
        ->hasDefinition($entityType)) {
        $entityTypeStorage = $this->entityTypeManager
          ->getStorage($entityType);
        $additionalEntityTypes = $entityTypeStorage
          ->loadMultiple();
        if (!empty($additionalEntityTypes)) {
          $form['additional_entity_type'][$entityType] = [
            '#type' => 'fieldset',
            '#title' => ucwords(str_replace('_', ' ', $entityType)),
          ];
          foreach ($additionalEntityTypes as $additionalEntityType => $additionalEntityTypesData) {
            $entityTypeName = str_replace('Default', '', $additionalEntityTypesData
              ->get('label'));
            $flag = 1;
            $form['additional_entity_type'][$entityType] += $this
              ->prepareEntityTypeForm($additionalEntityType, $entityTypeName, $config, ucwords(strtok($entityType, "_")));
          }
        }
      }
    }
    if ($flag == 1) {
      $form['additional_entity_type']['#description'] = $this
        ->t('List of additional entity types added from <a href="@link">Notifications Admin Settings</a>', [
        '@link' => Url::fromRoute('notifications_widget.notifications_widget_logger_settings')
          ->toString(),
      ]);
    }
    else {
      $form['additional_entity_type']['#description'] = $this
        ->t('There no additional entity types added into admin settings. You can add more entity type from <a href="@link">Notifications Admin Settings</a>', [
        '@link' => Url::fromRoute('notifications_widget.notifications_widget_logger_settings')
          ->toString(),
      ]);
    }
  }
  return parent::buildForm($form, $form_state);
}