You are here

LikeWidget.php in Votingapi Widgets 8

File

src/Plugin/votingapi_widget/LikeWidget.php
View source
<?php

namespace Drupal\votingapi_widgets\Plugin\votingapi_widget;

use Drupal\votingapi_widgets\Plugin\VotingApiWidgetBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Assigns ownership of a node to a user.
 *
 * @VotingApiWidget(
 *   id = "like",
 *   label = @Translation("Like"),
 *   values = {
 *    1 = @Translation("Like"),
 *   },
 * )
 */
class LikeWidget extends VotingApiWidgetBase {
  use StringTranslationTrait;

  /**
   * Vote form.
   */
  public function buildForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings) {
    $form = $this
      ->getForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings);
    $build = [
      'rating' => [
        '#theme' => 'container',
        '#attributes' => [
          'class' => [
            'votingapi-widgets',
            'like',
            $settings['readonly'] === 1 ? 'read_only' : '',
          ],
        ],
        '#children' => [
          'form' => $form,
        ],
      ],
      '#attached' => [
        'library' => [
          'votingapi_widgets/like',
        ],
      ],
    ];
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function getInitialVotingElement(array &$form) {
    $form['value']['#prefix'] = '<div class="votingapi-widgets like">';
    $form['value']['#attached'] = [
      'library' => [
        'votingapi_widgets/like',
      ],
    ];
    $form['value']['#suffix'] = '</div>';
  }

  /**
   * {@inheritdoc}
   */
  public function getStyles() {
    return [
      'default' => $this
        ->t('Default'),
    ];
  }

}

Classes

Namesort descending Description
LikeWidget Assigns ownership of a node to a user.