You are here

public function FivestarItem::fieldSettingsForm in Fivestar 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/FivestarItem.php, line 116

Class

FivestarItem
Plugin implementation of the 'fivestar' field type.

Namespace

Drupal\fivestar\Plugin\Field\FieldType

Code

public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  $element = [];
  $element['stars'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Number of stars'),
    '#options' => array_combine(range(1, 10), range(1, 10)),
    '#default_value' => $this
      ->getSetting('stars'),
  ];
  $element['allow_clear'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow users to cancel their ratings.'),
    '#default_value' => $this
      ->getSetting('allow_clear'),
    '#return_value' => 1,
  ];
  $element['allow_revote'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow users to re-vote on already voted content.'),
    '#default_value' => $this
      ->getSetting('allow_revote'),
    '#return_value' => 1,
  ];
  $element['allow_ownvote'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow users to vote on their own content.'),
    '#default_value' => $this
      ->getSetting('allow_ownvote'),
    '#return_value' => 1,
  ];
  $element['rated_while'] = [
    '#type' => 'radios',
    '#default_value' => $this
      ->getSetting('rated_while'),
    '#title' => $this
      ->t('Select when user can rate the field'),
    '#options' => [
      'viewing' => 'Rated while viewing',
      'editing' => 'Rated while editing',
    ],
  ];
  $element['enable_voting_target'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Set voting target'),
    '#default_value' => $this
      ->getSetting('enable_voting_target'),
  ];
  $states = [
    'visible' => [
      ':input[name="settings[enable_voting_target]"]' => [
        'checked' => TRUE,
      ],
    ],
    'required' => [
      ':input[name="settings[enable_voting_target]"]' => [
        'checked' => TRUE,
      ],
    ],
  ];
  $element['target_bridge_field'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Target bridge field'),
    '#description' => $this
      ->t('Machine name of field that binds current entity with entity that contain target fivestar field.
        The field should have "entity_reference" type.'),
    '#states' => $states,
    '#default_value' => $this
      ->getSetting('target_bridge_field'),
  ];
  $element['target_fivestar_field'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Target fivestar field'),
    '#description' => $this
      ->t('Machine name of fivestar field which should affect after vote.'),
    '#states' => $states,
    '#default_value' => $this
      ->getSetting('target_fivestar_field'),
  ];
  $element['#element_validate'] = [
    [
      get_class($this),
      'fieldSettingsFormValidate',
    ],
  ];

  // @todo try to find the way to omit it.
  $form_state
    ->set('host_entity', $this
    ->getEntity());
  return $element;
}