You are here

ResponsiveShareButtonsForm.php in Responsive Share Buttons 8

File

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

namespace Drupal\responsive_share_buttons\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Responsive share buttons configuration form.
 */
class ResponsiveShareButtonsForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('responsive_share_buttons.settings');

    // Get the current networks, sorted by weight.
    $networks = $config
      ->get('networks');
    uasort($networks, [
      'Drupal\\Component\\Utility\\SortArray',
      'sortByWeightElement',
    ]);
    $form['networks'] = [
      '#type' => 'table',
      '#id' => 'draggable-table',
      '#caption' => $this
        ->t('Networks for sharing.'),
      '#header' => [
        $this
          ->t('Network'),
        $this
          ->t('Name'),
        $this
          ->t('Active'),
        $this
          ->t('Weight'),
      ],
      '#tabledrag' => [
        [
          'table_id' => 'draggable-table',
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'table-order-weight',
        ],
      ],
    ];
    foreach ($networks as $name => $network) {
      $form['networks'][$name] = [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'name' => [
          '#type' => 'hidden',
          '#value' => $name,
        ],
        'network' => [
          '#plain_text' => $name,
        ],
        'active' => [
          '#type' => 'checkbox',
          '#title_display' => 'invisible',
          '#default_value' => isset($network['active']) ? $network['active'] : FALSE,
        ],
        'weight' => [
          '#type' => 'weight',
          '#title_display' => 'invisible',
          '#default_value' => isset($network['weight']) ? $network['weight'] : 0,
          '#attributes' => [
            'class' => [
              'table-order-weight',
            ],
          ],
        ],
      ];
    }
    $form['twitter_name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Twitter username'),
      '#default_value' => $config
        ->get('twitter_name', ''),
      '#description' => $this
        ->t('Add a twitter name for tweets to include a via mention'),
    ];
    return parent::buildForm($form, $form_state);
  }

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

    // Get the existing configuration.
    $config = $this
      ->config('responsive_share_buttons.settings');
    $networks = $config
      ->get('networks');
    $values = $form_state
      ->getValues();
    uasort($networks, [
      'Drupal\\Component\\Utility\\SortArray',
      'sortByWeightElement',
    ]);
    $config
      ->set('networks', $values['networks'])
      ->set('twitter_name', $form_state
      ->getValue('twitter_name'))
      ->save();
    drupal_flush_all_caches();
    parent::submitForm($form, $form_state);
  }

}

Classes

Namesort descending Description
ResponsiveShareButtonsForm Responsive share buttons configuration form.