Commit 91e4ffe1 authored by Daniel Cothran's avatar Daniel Cothran Committed by Daniel Cothran
Browse files

Issue #3277589 by andileco: Clean up code documentation, add return types, etc.

parent 46dd9f7c
Loading
Loading
Loading
Loading
+4 −4
Changes for modules/charts_billboard/README.md: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -14,13 +14,13 @@ If you use Composer to manage dependencies, edit your site's `composer.json` fil
            "type": "package",
            "package": {
                "name": "billboardjs/billboard",
                "version": "1.6.2",
                "version": "3.4.1",
                "type": "drupal-library",
                "extra": {
                    "installer-name": "billboard"
                },
                "dist": {
                    "url": "https://github.com/naver/billboard.js/archive/1.6.2.zip",
                    "url": "https://github.com/naver/billboard.js/archive/3.4.1.zip",
                    "type": "zip"
                }
            }
@@ -35,7 +35,7 @@ If you use Composer to manage dependencies, edit your site's `composer.json` fil
                    "installer-name": "d3"
                },
                "dist": {
                    "url": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.1/d3.min.js",
                    "url": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.1/d3.js",
                    "type": "file"
                },
                "require": {
@@ -44,4 +44,4 @@ If you use Composer to manage dependencies, edit your site's `composer.json` fil
            }
        }

  4. Run `composer require --prefer-dist billboardjs/billboard:1.6.2 d3/d3:4.9.1` - you should find that new directories have been created under "/libraries"
  4. Run `composer require --prefer-dist billboardjs/billboard:3.4.1 d3/d3:4.9.1` - you should find that new directories have been created under "/libraries"
+3 −3
Changes for modules/charts_billboard/charts_billboard.libraries.yml: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -18,14 +18,14 @@ d3:
  js:
    /libraries/d3/d3.min.js: {  }
billboard:
  remote: 'https://naver.github.io/billboard.js/release/latest/dist/billboard.min.js'
  version: 0.4.18
  remote: 'https://naver.github.io/billboard.js/release/3.4.1/dist/billboard.min.js'
  version: 3.4.1
  license:
    name: MIT
    url: 'https://opensource.org/licenses/MIT'
    gpl-compatible: true
  cdn:
    /libraries/billboard/: https://naver.github.io/billboard.js/release/latest/
    /libraries/billboard/: https://naver.github.io/billboard.js/release/3.4.1/
  css:
    theme:
      /libraries/billboard/dist/billboard.min.css: {  }
+64 −38
Changes for src/Plugin/views/style/ChartsPluginStyleChart.php: 64 added lines, 38 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ namespace Drupal\charts\Plugin\views\style;
use Drupal\charts\Plugin\chart\Library\ChartInterface;
use Drupal\charts\ChartManager;
use Drupal\charts\TypeManager;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
@@ -38,7 +39,7 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;
  protected ConfigFactoryInterface $configFactory;

  /**
   * Fields.
@@ -59,35 +60,35 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
   *
   * @var \Drupal\charts\ChartManager
   */
  protected $chartManager;
  protected ChartManager $chartManager;

  /**
   * The chart type manager.
   *
   * @var \Drupal\charts\TypeManager
   */
  protected $chartTypeManager;
  protected TypeManager $chartTypeManager;

  /**
   * The label field key.
   *
   * @var \Drupal\charts\Plugin\views\style\ChartsPluginStyleChart
   * @var string
   */
  protected $labelFieldKey;
  protected string $labelFieldKey;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;
  protected RouteMatchInterface $routeMatch;

  /**
   * The Twig environment.
   *
   * @var \Drupal\Core\Template\TwigEnvironment
   */
  protected $twig;
  protected TwigEnvironment $twig;

  /**
   * Constructs a ChartsPluginStyleChart object.
@@ -137,7 +138,7 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
  protected function defineOptions(): array {
    $options = parent::defineOptions();
    $charts_settings = $this->configFactory->get('charts.settings');
    $charts_default_settings = $charts_settings->get('charts_default_settings') ?? [];
@@ -210,7 +211,7 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
  /**
   * {@inheritdoc}
   */
  public function validate() {
  public function validate(): array {
    $errors = parent::validate();
    $chart_settings = $this->options['chart_settings'];
    $selected_data_fields = !empty($chart_settings['fields']['data_providers']) && is_array($chart_settings['fields']['data_providers']) ? $this->getSelectedDataFields($chart_settings['fields']['data_providers']) : NULL;
@@ -226,7 +227,7 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
  /**
   * {@inheritdoc}
   */
  public function render() {
  public function render(): array {
    $field_handlers = $this->view->getHandlers('field');
    $chart_settings = $this->options['chart_settings'];
    $chart_fields = $chart_settings['fields'];
@@ -478,7 +479,7 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
  /**
   * {@inheritdoc}
   */
  public function renderGrouping($records, $groupings = [], $group_rendered = NULL) {
  public function renderGrouping($records, $groupings = [], $group_rendered = NULL): mixed {
    if (empty($this->options['grouping'])) {
      return parent::renderGrouping($records, $groupings, $group_rendered);
    }
@@ -549,24 +550,13 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
    return $sets;
  }

  /**
   * Utility function to check if this chart has a parent display.
   *
   * @return bool
   *   Parent Display.
   */
  public function getParentChartDisplay() {
    $parent_display = FALSE;
    return $parent_display;
  }

  /**
   * Utility function to check if this chart has children displays.
   *
   * @return array
   *   Children Chart Display.
   */
  public function getChildrenChartDisplays() {
  public function getChildrenChartDisplays(): array {
    $children_displays = $this->displayHandler->getAttachedDisplays();
    foreach ($children_displays as $key => $child) {
      $display_handler = $this->view->displayHandlers->get($child);
@@ -582,7 +572,7 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
  public function calculateDependencies(): array {
    $dependencies = [];

    if (!empty($this->options['library'])) {
@@ -604,7 +594,7 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
   * @return \Drupal\Component\Render\MarkupInterface|float|null
   *   The value.
   */
  public function processNumberValueFromField($number, $field) {
  public function processNumberValueFromField($number, $field): float|MarkupInterface|null {
    if (is_array($this->getField($number, $field))) {
      $value = $this->getField($number, $field)->__toString();
    }
@@ -639,14 +629,20 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
   * @return array
   *   The fields.
   */
  private function getSelectedDataFields(array $data_providers) {
  private function getSelectedDataFields(array $data_providers): array {
    return array_filter($data_providers, function ($value) {

      return !empty($value['enabled']);
    });
  }

  private function getLabelFieldKey() {
  /**
   * Returns the key of the Label Field.
   *
   * @return string
   *   The Label Field key.
   */
  private function getLabelFieldKey(): string {
    if (!isset($this->labelFieldKey)) {
      $field_handlers = $this->view->getHandlers('field');
      $chart_settings = $this->options['chart_settings'];
@@ -654,10 +650,23 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
      $label_field = $field_handlers[$chart_fields['label']] ?? '';
      $this->labelFieldKey = $label_field ? $chart_fields['label'] : '';
    }

    return $this->labelFieldKey;
  }

  private function groupedChartElementBuild(&$chart, $sets, $data_fields) {
  /**
   * Helper method to build the chart data elements of a grouped chart.
   *
   * @param array $chart
   *   The main chart element.
   * @param array $sets
   *   The grouped record set.
   * @param array $data_fields
   *   The selected data fields on the chart style options.
   *
   * @return void
   */
  private function groupedChartElementBuild(array &$chart, array $sets, array $data_fields): void {
    $original_xaxis = $chart['xaxis'];
    $xaxis_labels = [];
    $label_field_key = $this->getLabelFieldKey();
@@ -725,11 +734,21 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
   * @return array
   *   The render array of the chart settings.
   */
  public static function groupingChartSettingsAjaxCallback(array $form, FormStateInterface $form_state) {
  public static function groupingChartSettingsAjaxCallback(array $form, FormStateInterface $form_state): array {
    return $form['options']['style_options']['chart_settings'];
  }

  private function extractGroupedSelectedColorByEntity(EntityField $view_entity_field, ResultRow $row, string $group_field_name) {
  /**
   * Returns the selected color.
   *
   * @param \Drupal\views\Plugin\views\field\EntityField $view_entity_field
   * @param \Drupal\views\ResultRow $row
   * @param string $group_field_name
   *
   * @return string
   *   The color.
   */
  private function extractGroupedSelectedColorByEntity(EntityField $view_entity_field, ResultRow $row, string $group_field_name): string {
    $chart_settings = $this->options['chart_settings'];
    $colors_settings = $chart_settings['fields']['entity_grouping']['selected_method']['colors'] ?? [];
    /** @var \Drupal\Core\Entity\ContentEntityInterface $host_entity */
@@ -746,7 +765,17 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
    return $colors_settings[$color_id_key]['color'] ?? '';
  }

  private function extractGroupedSelectedColorOnReferencedEntityField(EntityField $view_entity_field, ResultRow $row, string $group_field_name) {
  /**
   * Returns the color from the referenced entity field.
   *
   * @param \Drupal\views\Plugin\views\field\EntityField $view_entity_field
   * @param \Drupal\views\ResultRow $row
   * @param string $group_field_name
   *
   * @return string
   *   The color.
   */
  private function extractGroupedSelectedColorOnReferencedEntityField(EntityField $view_entity_field, ResultRow $row, string $group_field_name): string {
    $chart_settings = $this->options['chart_settings'];
    $color_field_name = $chart_settings['fields']['entity_grouping']['selected_method']['color_field_name'] ?? '';
    if (!$color_field_name) {
@@ -769,17 +798,14 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
  /**
   * Ensures chart attachments are placed correctly on chart.
   *
   * @param array
   *   $parent_labels
   * @param array
   *   $child_mapped_data
   * @param array
   *   $data
   * @param array $parent_labels
   * @param array $child_mapped_data
   * @param array $data
   *
   * @return array
   *   $processed_data
   */
  private function alignSubchartData(array $parent_labels, array $child_mapped_data, array $data) {
  private function alignSubchartData(array $parent_labels, array $child_mapped_data, array $data): array {
    $child_labels = array_keys($child_mapped_data);
    if ($parent_labels === $child_labels) {
      return $data;