You are here

function votingapi_reaction_form_alter in Voting API Reaction 8

Implements hook_form_alter().

File

./votingapi_reaction.module, line 76
Allows users to react to any entity using Voting and Field APIs.

Code

function votingapi_reaction_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (in_array($form_id, [
    'vote_type_add_form',
    'vote_type_edit_form',
  ])) {

    /* @var \Drupal\votingapi\Entity\VoteType $entity */
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    $form['reaction'] = [
      '#type' => 'checkbox',
      '#title' => t('Use as a reaction'),
      '#description' => t('If checked, this vote type will be considered as a reaction and used by Voting API Reaction module.'),
      '#default_value' => $entity
        ->getThirdPartySetting('votingapi_reaction', 'reaction'),
    ];
    $form['icon'] = [
      '#type' => 'fieldset',
      '#title' => t('Reaction settings'),
      '#states' => [
        'visible' => [
          ':input[name="reaction"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $default = TRUE;
    $image = [
      '#type' => 'html_tag',
      '#tag' => 'img',
      '#attributes' => [
        'src' => \Drupal::service('votingapi_reaction.manager')
          ->getIcon($entity, $default),
        'width' => 30,
        'alt' => t('Current icon for this reaction'),
      ],
    ];
    $form['icon']['current'] = [
      '#type' => 'item',
      '#title' => t('Current'),
      '#markup' => \Drupal::service('renderer')
        ->render($image),
    ];
    if (!$default) {
      $form['icon']['remove'] = [
        '#type' => 'checkbox',
        '#title' => t('Remove'),
        '#description' => t('If checked, current icon will be removed. You can upload a new one using the field below.'),
      ];
    }
    $form['icon']['new'] = [
      '#type' => 'managed_file',
      '#title' => $default ? t('Upload') : t('Replace'),
      '#description' => t('Image will be used by Voting API Reaction module as an icon for reaction.'),
      '#upload_location' => 'public://votingapi_reaction',
      '#upload_validators' => [
        'file_validate_extensions' => [
          'svg',
          'png',
        ],
      ],
    ];
    $form['actions']['submit']['#submit'][] = 'votingapi_reaction_form_vote_type_form_submit';
  }
}