You are here

function select2boxes_field_widget_third_party_settings_form in Select2 Boxes 8

Implements hook_field_widget_third_party_settings_form().

File

./select2boxes.module, line 73
Basic module file.

Code

function select2boxes_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition) {

  // Get all third party settings provided by this module.
  $settings = $plugin
    ->getThirdPartySettings('select2boxes');

  // Add the options, specific for the multiple entity reference widgets.
  if ($plugin
    ->getPluginId() == 'select2boxes_autocomplete_multi') {

    // Add preloading option to the widget settings form.
    $element['enable_preload'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable pre-loaded entries'),
      '#default_value' => isset($settings['enable_preload']) && $settings['enable_preload'] == '1',
    ];
    $field_name = $field_definition
      ->getName();
    $element['preload_count'] = [
      '#type' => 'textfield',
      '#title' => t('Maximum number of entries that will be pre-loaded'),
      '#description' => t('If maximum number is not specified then all entries will be preloaded'),
      '#default_value' => isset($settings['preload_count']) ? $settings['preload_count'] : '',
      '#states' => [
        'visible' => [
          ":input[name=\"fields[{$field_name}][settings_edit_form][third_party_settings][select2boxes][enable_preload]\"]" => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    return $element;
  }
  elseif ($plugin
    ->getPluginId() == 'select2boxes_autocomplete_list' || $plugin
    ->getPluginId() == 'select2boxes_autocomplete_single') {

    // Ignore fields with auto-creation option enabled.
    if (isset($field_definition
      ->getSetting('handler_settings')['auto_create'])) {
      return [];
    }

    // Add an option to allow users to include the flag icons.
    $types = [
      'language_field',
      'language',
      'country',
    ];
    if (in_array($field_definition
      ->getType(), $types) && \Drupal::moduleHandler()
      ->moduleExists('flags')) {
      $element['enable_flags'] = [
        '#type' => 'checkbox',
        '#title' => t('Include flags icons'),
        '#default_value' => isset($settings['enable_flags']) && $settings['enable_flags'] == '1',
      ];
      return $element;
    }
  }
  elseif (in_array($plugin
    ->getPluginId(), _select2boxes_address_widgets_list())) {
    $element['enable_select2'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable Select2 widget for dropdowns'),
      '#default_value' => isset($settings['enable_select2']) && $settings['enable_select2'] == '1',
    ];
    return $element;
  }
  return [];
}