You are here

public function DashboardSettingsForm::buildForm in Content Planner 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/DashboardSettingsForm.php, line 67

Class

DashboardSettingsForm
Defines a form that configures forms module settings.

Namespace

Drupal\content_planner\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {

  // Get block configurations from settings.
  $block_configurations = $this->dashboardSettingsService
    ->getBlockConfigurations();

  // Get registered Plugins.
  $plugins = $this->dashboardBlockPluginManager
    ->getDefinitions();
  uksort($plugins, function ($a, $b) {
    return strnatcmp($a, $b);
  });
  $block_options = [];

  // Loop over every plugin and generate renderable array.
  foreach ($plugins as $plugin_id => $plugin) {
    $block_options[$plugin_id] = $plugin['name'];
    if (array_key_exists($plugin_id, $block_configurations)) {
      $config_link = Link::createFromRoute($this
        ->t('Configure'), 'content_planner.dashboard_block_config_form', [
        'block_id' => $plugin_id,
      ], [
        'query' => [
          'destination' => Url::fromRoute('content_planner.dashboard_settings')
            ->toString(),
        ],
      ]);
      $block_options[$plugin_id] .= ' (' . $config_link
        ->toString() . ')';
    }
  }

  // Default value.
  if ($block_configurations) {
    $default_value = array_keys($block_configurations);
  }
  else {
    $default_value = [];
  }
  $form['available_dashboard_blocks'] = [
    '#type' => 'fieldset',
    '#title' => t('Dashboard Widgets'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  ];
  $form['available_dashboard_blocks']['enabled_blocks'] = [
    '#type' => 'checkboxes',
    '#title' => t('Available Dashboard Widgets'),
    '#description' => t('Select which Widgets should be displayed in the Dashboard.'),
    // '#required' => TRUE,.
    '#options' => $block_options,
    '#default_value' => $default_value,
  ];
  return parent::buildForm($form, $form_state);
}