You are here

public function ShareEverywhereConfigForm::buildForm in Share Everywhere 8

Same name and namespace in other branches
  1. 2.x src/Form/ShareEverywhereConfigForm.php \Drupal\share_everywhere\Form\ShareEverywhereConfigForm::buildForm()

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/ShareEverywhereConfigForm.php, line 123

Class

ShareEverywhereConfigForm
Provides a settings form for Share Everywhere module.

Namespace

Drupal\share_everywhere\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('share_everywhere.settings');
  $entity_bundles = $this->entityTypeBundleInfo
    ->getBundleInfo('node');
  $entity_view_modes = $this->entityDisplayRepository
    ->getViewModes('node');
  $view_modes = [];
  $content_types = [];
  $commerce_product = $this->moduleHandler
    ->moduleExists('commerce_product');
  if ($commerce_product) {
    $product_entity_bundles = $this->entityTypeBundleInfo
      ->getBundleInfo('commerce_product');
    $product_entity_view_modes = $this->entityDisplayRepository
      ->getViewModes('commerce_product');
    $product_types = [];
    $product_view_modes = [];
  }
  $form['buttons'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Share Everywhere Buttons'),
    '#description' => $this
      ->t('Enable/disable individual buttons.'),
  ];
  $form['buttons']['title'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'clearfix',
      ],
    ],
  ];
  $form['buttons']['title']['title_text'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#default_value' => $config
      ->get('title'),
  ];
  $form['buttons']['title']['display_title'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display title'),
    '#default_value' => $config
      ->get('display_title'),
  ];
  $share_buttons = $config
    ->get('buttons');
  uasort($share_buttons, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
  $form['buttons']['facebook_like'] = [
    '#type' => 'radios',
    '#parents' => [
      'buttons',
      'facebook_like',
      'enabled',
    ],
    '#title' => $share_buttons['facebook_like']['name'],
    '#options' => [
      1 => $this
        ->t('Enable'),
      0 => $this
        ->t('Disable'),
    ],
    '#default_value' => $share_buttons['facebook_like']['enabled'],
    '#description' => $this
      ->t('The like button will always be before or after the other share buttons, depending on the alignment setting.'),
  ];
  $form['buttons']['table'] = [
    '#type' => 'table',
    '#header' => [
      'Button',
      'Weight',
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'buttons-order-weight',
      ],
    ],
  ];
  foreach ($share_buttons as $key => $button) {
    if ($key != 'facebook_like') {
      $form['buttons']['table'][$key]['button'] = [
        '#type' => 'checkbox',
        '#parents' => [
          'buttons',
          $key,
          'enabled',
        ],
        '#title' => $button['name'],
        '#default_value' => $button['enabled'],
      ];
      $form['buttons']['table'][$key]['#attributes']['class'][] = 'draggable';
      $form['buttons']['table'][$key]['#weight'] = !empty($button['weight']) ? $button['weight'] : 0;
      $form['buttons']['table'][$key]['weight'] = [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for @title', [
          '@title' => $button['name'],
        ]),
        '#title_display' => 'invisible',
        '#parents' => [
          'buttons',
          $key,
          'weight',
        ],
        '#default_value' => !empty($button['weight']) ? $button['weight'] : 0,
        '#attributes' => [
          'class' => [
            'buttons-order-weight',
          ],
        ],
      ];
    }
  }
  $form['display'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Display settings'),
    '#description' => $this
      ->t('Configure where the Share Everywhere module should appear.'),
  ];
  $form['display']['style'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Style'),
    '#options' => [
      'share_everywhere' => $this
        ->t('Share Everywhere'),
      'custom' => $this
        ->t('Custom'),
    ],
    '#description' => $this
      ->t('Select the style of the buttons.'),
    '#default_value' => $config
      ->get('style'),
  ];
  $form['display']['libraries'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Libraries'),
    '#options' => [
      'include_css' => $this
        ->t('Include default CSS.'),
      'include_js' => $this
        ->t('Include default JavaScript.'),
    ],
    '#description' => $this
      ->t('Select which libraries to include.'),
    '#default_value' => [
      $config
        ->get('include_css'),
      $config
        ->get('include_js'),
    ],
    '#states' => [
      'visible' => [
        'input[name="style"]' => [
          'value' => 'custom',
        ],
      ],
    ],
  ];
  $form['display']['collapsible'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Collapsible'),
    '#options' => [
      0 => $this
        ->t('Always open'),
      1 => $this
        ->t('Collapsible'),
    ],
    '#description' => $this
      ->t('Select whether the buttons should be always visible or their container can be closed and opened.'),
    '#default_value' => $config
      ->get('collapsible'),
    '#states' => [
      'visible' => [
        [
          'input[name="style"]' => [
            'value' => 'share_everywhere',
          ],
        ],
        'or',
        [
          'input[name="libraries[include_js]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ],
  ];
  $form['display']['location'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Location'),
    '#options' => [
      'content' => $this
        ->t('Node content'),
      'links' => $this
        ->t('Links area'),
    ],
    '#description' => $this
      ->t('Select the location where the buttons will appear.'),
    '#default_value' => $config
      ->get('location'),
  ];
  $form['display']['alignment'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Alignment'),
    '#options' => [
      'left' => $this
        ->t('Align left'),
      'right' => $this
        ->t('Align right'),
    ],
    '#description' => $this
      ->t('Select which side of the page will the buttons appear.'),
    '#default_value' => $config
      ->get('alignment'),
  ];
  $form['display']['per_entity'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable per entity configuration'),
    '#description' => $this
      ->t('If you turn on this setting then you will need to enable Share Everywhere on every entity that is of the type selected below.'),
    '#default_value' => $config
      ->get('per_entity'),
  ];
  $form['display']['visibility'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Visibility'),
  ];
  $form['display']['content'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Content types'),
    '#group' => 'visibility',
  ];
  $form['display']['views'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('View modes'),
    '#group' => 'visibility',
  ];
  foreach ($entity_view_modes as $mode => $mode_info) {
    $view_modes[$mode] = $mode_info['label'];
  }
  foreach ($entity_bundles as $bundle => $bundle_info) {
    if ($config
      ->get('view_modes.' . $bundle) == NULL) {
      $config
        ->set('view_modes.' . $bundle, [
        'full' => 'full',
      ])
        ->save();
    }
    $form['display']['views'][$bundle . '_options'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('%label View Modes', [
        '%label' => $bundle_info['label'],
      ]),
      '#description' => $this
        ->t('Select which view modes the Share Everywhere module should appear on for %label nodes.', [
        '%label' => $bundle_info['label'],
      ]),
      '#options' => $view_modes,
      '#default_value' => $config
        ->get('view_modes.' . $bundle),
    ];
    $content_types[$bundle] = $bundle_info['label'];
  }
  $form['display']['content']['content_types'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Content types'),
    '#description' => $this
      ->t('Select which content types the Share Everywhere module should appear on.'),
    '#options' => $content_types,
    '#default_value' => $config
      ->get('content_types'),
  ];
  if ($commerce_product) {
    $form['display']['product'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Product types'),
      '#group' => 'visibility',
    ];
    $form['display']['product_views'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Product view modes'),
      '#group' => 'visibility',
    ];
    foreach ($product_entity_view_modes as $mode => $mode_info) {
      $product_view_modes[$mode] = $mode_info['label'];
    }
    if (!isset($product_view_modes['full'])) {
      $product_view_modes = [
        'full' => $this
          ->t('Full'),
      ] + $product_view_modes;
    }
    foreach ($product_entity_bundles as $bundle => $bundle_info) {
      if ($config
        ->get('product_view_modes.' . $bundle) == NULL) {
        $config
          ->set('product_view_modes.' . $bundle, [
          'full' => 'full',
        ])
          ->save();
      }
      $form['display']['product_views'][$bundle . '_options'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('%label View Modes', [
          '%label' => $bundle_info['label'],
        ]),
        '#description' => $this
          ->t('Select which view modes the Share Everywhere module should appear on for %label products.', [
          '%label' => $bundle_info['label'],
        ]),
        '#options' => $product_view_modes,
        '#default_value' => $config
          ->get('product_view_modes.' . $bundle),
      ];
      $product_types[$bundle] = $bundle_info['label'];
    }
    $form['display']['product']['product_types'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Product types'),
      '#description' => $this
        ->t('Select which product types the Share Everywhere module should appear on.'),
      '#options' => $product_types,
      '#default_value' => $config
        ->get('product_types'),
    ];
  }
  $form['display']['request_path'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Pages'),
    '#group' => 'visibility',
  ];
  $pages = is_array($config
    ->get('restricted_pages.pages')) ? implode("\r\n", $config
    ->get('restricted_pages.pages')) : $config
    ->get('restricted_pages.pages');
  $form['display']['request_path']['pages'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Pages'),
    '#default_value' => $pages,
    '#description' => $this
      ->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. An example path is %user-wildcard for every user page. %front is the front page.", [
      '%user-wildcard' => '/user/*',
      '%front' => '<front>',
    ]),
  ];
  $form['display']['request_path']['type'] = [
    '#type' => 'radios',
    '#options' => [
      'show' => $this
        ->t('Show for the listed pages'),
      'hide' => $this
        ->t('Hide for the listed pages'),
    ],
    '#default_value' => $config
      ->get('restricted_pages.type'),
  ];
  return parent::buildForm($form, $form_state);
}