starry_rating-1.0.x-dev/src/Plugin/Field/FieldWidget/RatingWidget.php

src/Plugin/Field/FieldWidget/RatingWidget.php
<?php

declare(strict_types=1);

namespace Drupal\starry_rating\Plugin\Field\FieldWidget;

use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines the 'starry_star_rating' field widget.
 */
#[FieldWidget(
  id: 'starry_star_rating',
  label: new TranslatableMarkup('Rating'),
  field_types: ['starry_rating']
)]
class RatingWidget extends WidgetBase {

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state): array {

    $value = $items[$delta]->value ?? 0;

    $element['value'] = [
      '#type' => 'hidden',
      '#default_value' => $value,
      '#attributes' => [
        'class' => ['star-rating-input'],
      ],
    ];

    $element['#attached']['library'][] = 'starry_rating/star_rating';

    $element['star_display'] = [
      '#markup' => '<div class="star-rating" data-read-only="false" data-stars="0"></div>',
    ];

    return $element;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc