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

src/Plugin/views/style/WorkTimeSheet.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 = "worktime_sheet",
 *   title = @Translation("Monthly work timesheet"),
 *   help = @Translation("Render a list of worktime by project in month."),
 *   theme = "views_view_work_time_sheet",
 *   display_types = { "normal" }
 * )
 */
class WorkTimeSheet 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['reference_id'] = ['default' => ''];
    $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];
    }
    $form['reference_id'] = [
      '#type' => 'select',
      '#title' => $this->t('Entity id field'),
      '#description' => $this->t('Select project id field'),
      '#options' => $type["number_integer"],
      '#required' => TRUE,
      '#default_value' => $this->options['reference_id'] ?? '',
    ];

    $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['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 custom data ajax load'),
      '#description' => $this->t('Array object. Example [{date:"2023-01-20",uid:1,total_time:8,reference_id:2}]'),
      '#default_value' => $this->options['custom_data'] ?? '',
    ];

  }

}

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

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