You are here

public function VotingApiReactionItem::fieldSettingsForm in Voting API Reaction 8

Returns a form for the field-level settings.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::fieldSettingsForm

File

src/Plugin/Field/FieldType/VotingApiReactionItem.php, line 77

Class

VotingApiReactionItem
Plugin implementation of the 'votingapi_reaction' field type.

Namespace

Drupal\votingapi_reaction\Plugin\Field\FieldType

Code

public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  $element = [
    '#attached' => [
      'library' => [
        'votingapi_reaction/settings_styles',
      ],
    ],
  ];
  $element['anonymous_detection'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Anonymous detection method'),
    '#description' => $this
      ->t("By which method anonymous users must be detected. Warning: Detecting users by cookies is not a reliable way, as cookies could be easily manipulated by users."),
    '#options' => [
      VotingApiReactionItemInterface::BY_COOKIES => $this
        ->t('By cookies'),
      VotingApiReactionItemInterface::BY_IP => $this
        ->t('By IP'),
    ],
    '#default_value' => $this
      ->getSetting('anonymous_detection'),
    '#required' => TRUE,
  ];
  $options = [
    300,
    900,
    1800,
    3600,
    10800,
    21600,
    32400,
    43200,
    86400,
    172800,
    345600,
    604800,
  ];
  foreach ($options as $key => $option) {
    unset($options[$key]);
    $options[$option] = \Drupal::service('date.formatter')
      ->formatInterval($option);
  }
  $options[VotingApiReactionItemInterface::NEVER_ROLLOVER] = $this
    ->t('Never');
  $options[VotingApiReactionItemInterface::VOTINGAPI_ROLLOVER] = $this
    ->t('Voting API Default');
  $element['anonymous_rollover'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Anonymous vote rollover'),
    '#description' => $this
      ->t("The amount of time that must pass before two anonymous votes from the same computer are considered unique. Setting this to 'never' will eliminate most double-voting, but will make it impossible for multiple anonymous on the same computer (like internet cafe customers) from casting votes."),
    '#options' => $options,
    '#default_value' => $this
      ->getSetting('anonymous_rollover'),
  ];
  $reactionManager = \Drupal::service('votingapi_reaction.manager');
  $element['reactions'] = [
    '#title' => $this
      ->t('Reactions'),
    '#type' => 'checkboxes',
    '#required' => TRUE,
    '#options' => $reactionManager
      ->getReactions([
      'show_icon' => TRUE,
      'show_label' => TRUE,
      'show_count' => FALSE,
      'sort_reactions' => 'none',
      'reactions' => array_keys($reactionManager
        ->allReactions()),
    ], []),
    '#default_value' => $this
      ->getSetting('reactions'),
    '#attributes' => [
      'class' => [
        'votingapi-reaction-settings',
      ],
    ],
  ];
  return $element;
}