You are here

AdminSettings.php in Calendar Systems 8.2

Contains Drupal\CalendarSystems\Form\AdminSettings.

File

src/Form/AdminSettings.php
View source
<?php

/**
 * @file
 * Contains Drupal\CalendarSystems\Form\AdminSettings.
 */
namespace Drupal\calendar_systems\Form;

use Drupal\calendar_systems\Helpers;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class AdminSettings extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'calendar_systems.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'calendar_systems_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('calendar_systems.settings');
    Helpers::calendar_systems_add_strings();
    if (Helpers::calendar_systems_is_patch_applied() !== TRUE) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('There is a problem with calendar systems installation, please visit <a href=":readme-link">site status page</a> for more information.', [
        ':readme-link' => $this
          ->getUrlGenerator()
          ->generateFromRoute('system.status'),
      ]));
    }

    // build the overview form array.
    $form['profiles'] = array();
    $form['profiles']['#type'] = 'table';

    //$form['profiles']['#caption'] = $this->t('Sample Table');
    $form['profiles']['#header'] = [
      $this
        ->t('Language'),
      $this
        ->t('Calendar system'),
      $this
        ->t('Operations'),
    ];

    // Set initials.
    $profiles = Helpers::calendar_systems_profiles();

    //print_r($profiles);
    $languages = Helpers::calendar_systems_langauges();

    //$calendar_systems = Helpers::_calendar_systems_plugins();
    $options = Helpers::calendar_systems_profiles_simple();

    // Table rows:
    foreach ($languages as $id => $language) {

      // Language:
      $form['profiles'][$id]['language'] = array(
        '#type' => 'markup',
        '#plain_text' => $language['name'],
      );

      // Calendar systems:
      $form['profiles'][$id]['calendar_system'] = array(
        '#type' => 'select',
        '#options' => $options,
        '#id' => "edit-editor-{$id}",
        '#default_value' => isset($profiles[$id]) ? $profiles[$id]['calendar_system'] : '',
        '#disabled' => isset($profiles[$id]) ? (bool) $profiles[$id]['calendar_system'] : FALSE,
      );

      // Operation links:
      if (isset($profiles[$id]) && !empty($profiles[$id]['calendar_system'])) {
        $form['profiles'][$id]['remove'] = array(
          '#type' => 'markup',
          '#markup' => $this
            ->getLinkGenerator()
            ->generate($this
            ->t('Remove'), new Url("calendar_systems.admin.profiles.delete", [
            'id' => $id,
          ])),
        );
      }
    }
    $form['form_fields_date_picker'] = array(
      '#type' => 'fieldset',
      '#title' => t('Javascript Date Picker'),
    );
    if (\Drupal::moduleHandler()
      ->moduleExists('jquery_calendar')) {
      $form['form_fields_date_picker']['core_text_date_fields'] = array(
        '#type' => 'checkbox',
        '#title' => t('Add to text date fields'),
        '#default_value' => $config
          ->get('js_date_picker_core_text_date_fields'),
      );
    }
    else {
      $form['form_fields_date_picker']['note'] = array(
        '#type' => 'markup',
        '#markup' => t('You must enable %s module in order to be able to use this feature', array(
          '%s' => 'jquery calendars',
        )),
      );
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Retrieve the configuration
    $this->configFactory
      ->getEditable('calendar_systems.settings')
      ->set('js_date_picker_core_text_date_fields', $form_state
      ->getValue('js_date_picker_core_text_date_fields', 0))
      ->save();
    Helpers::calendar_systems_profiles_update($form_state
      ->getValue('profiles', []));
    parent::submitForm($form, $form_state);
  }

}

Classes

Namesort descending Description
AdminSettings