work_time-1.0.x-dev/src/Plugin/views/style/WorkTimekeeper.php

src/Plugin/views/style/WorkTimekeeper.php
<?php

namespace Drupal\work_time\Plugin\views\style;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;

/**
 * Style plugin to render a list of work time in months.
 *
 * @ingroup views_style_plugins
 *
 * @ViewsStyle(
 *   id = "work_timekeeper",
 *   title = @Translation("Monthly work timekeeper"),
 *   help = @Translation("Render a list of work timekeeper in month."),
 *   theme = "views_view_work_timekeeper",
 *   display_types = { "normal" }
 * )
 */
class WorkTimekeeper extends StylePluginBase {

  /**
   * {@inheritdoc}
   */
  protected $usesRowPlugin = TRUE;

  /**
   * Does the style plugin support custom css class for the rows.
   *
   * @var bool
   */
  protected $usesRowClass = TRUE;

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['price_field'] = ['default' => ''];
    $options['holidays'] = ['default' => '01-01,01-05'];
    $options['custom_data'] = ['default' => ''];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    unset($form["grouping"]);
    $fields = $this->displayHandler->getHandlers('field');
    $labels = $this->displayHandler->getFieldLabels();
    $field_labels = [];

    foreach ($fields as $field_name => $field) {
      if (!empty($field->options["type"])) {
        $type[$field->options["type"]][$field_name] = $labels[$field_name];
      }
      $field_labels[$field_name] = $labels[$field_name];
    }
    $type["number_decimal"] = $type["number_decimal"] ?? [];
    $type["number_integer"] = $type["number_integer"] ?? [];
    $form['price_field'] = [
      '#type' => 'select',
      '#title' => $this->t('Unit price field'),
      '#description' => $this->t('Select unit price field'),
      "#empty_option" => $this->t("- Select -"),
      '#options' => array_merge($type["number_decimal"], $type["number_integer"]),
      '#default_value' => $this->options['price_field'] ?? '',
    ];
    $form['filter_time'] = [
      '#type' => 'select',
      '#title' => $this->t('Filter by'),
      '#description' => $this->t('Filter time total, use query example for month ?date=@month or for week ?date=@week or for year ?date=@year', [
        '@month' => date('Y-m'),
        '@week' => date('Y-\WW'),
        '@year' => date('Y'),
      ]),
      '#options' => [
        'month' => $this->t('Month'),
        'week' => $this->t('Week'),
        'year' => $this->t('Year'),
      ],
      '#required' => TRUE,
      '#default_value' => $this->options['filter_time'] ?? '',
    ];
    $form['holidays'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Holidays'),
      '#description' => $this->t('Format d-m and separated by ,. Example 01-01, 01-05'),
      '#default_value' => $this->options['holidays'] ?? '',
    ];

    $form['custom_data'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Url for leave data ajax load'),
      '#description' => $this->t('Array object. Example [{date:"2023-01-20",uid:1,total_time:8,price:15}]'),
      '#default_value' => $this->options['custom_data'] ?? '',
    ];
  }

}

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

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