Commit cbdd3905 authored by captainjak's avatar captainjak
Browse files

Issue #3294605: Correct errors given by phpcs

parent 2dd318c2
Loading
Loading
Loading
Loading
+1 −2
Changes for optional_date.info.yml: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
name: 'Optional Date'
description: ''
version: 1.0.0
description: 'Enhances date functionality to support optional times and optional end dates in ranges.'
core_version_requirement: ^9
type: module
+19 −13
Changes for src/Element/OptionalDate.php: 19 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -7,9 +7,12 @@ use Drupal\Core\Datetime\Element\Datetime;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides an optional_date element.
 *
 * @FormElement("optional_date")
 */
class OptionalDate extends Datetime {

  /**
   * Defines the timezone that dates should be stored in.
   */
@@ -36,7 +39,6 @@ class OptionalDate extends Datetime {
   * {@inheritdoc}
   */
  public static function validateDatetime(&$element, FormStateInterface $form_state, &$complete_form) {
    $errorMessage = NULL;
    $value = NestedArray::getValue($form_state->getValues(), $element['#parents']);

    if (!is_array($value)) {
@@ -47,25 +49,28 @@ class OptionalDate extends Datetime {
      return;
    }

    $dateFormat = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : '';
    $timeFormat = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : '';

    $format = static::formatExample(trim($dateFormat . ' ' . $timeFormat));
    $message = NULL;
    $title = !empty($element['#title']) ? $element['#title'] : '';

    if ($value['object'] === NULL) {
      $errorMessage = 'The %title date is invalid.';
      $message = t('The %title date is invalid. The expected format is %format.',
        ['%title' => $title, '%format' => $format]);
    }
    elseif (empty($value['date']) && $element['#required']) {
      $errorMessage = 'The %title date is required.';
      $message = t('The %title date is required. The expected format is %format.',
        ['%title' => $title, '%format' => $format]);
    }
    elseif (empty($value['time']) && !$element['#date_time_is_optional']) {
      $errorMessage = 'The %title time is required.';
      $message = t('The %title time is required. The expected format is %format.',
        ['%title' => $title, '%format' => $format]);
    }

    if ($errorMessage) {
      $date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : '';
      $time_format = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : '';

      $errorMessage = $errorMessage . ' The expected format is %format.';
      $format = static::formatExample(trim($date_format . ' ' . $time_format));
      $title = !empty($element['#title']) ? $element['#title'] : '';

      $form_state->setError($element, t($errorMessage, ['%title' => $title, '%format' => $format]));
    if ($message) {
      $form_state->setError($element, $message);
    }
  }

@@ -109,4 +114,5 @@ class OptionalDate extends Datetime {

    return $info;
  }

}

src/OptionalDateComputed.php

deleted100644 → 0
+0 −52
Changes for src/OptionalDateComputed.php: 0 added lines, 52 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\optional_date;

use Drupal\Core\TypedData\TypedData;

/**
 *
 */
class OptionalDateComputed extends TypedData {
  /**
   *
   */
  protected $timestamp = NULL;

  /**
   * {@inheritdoc}
   */
  public function getValue() {
    if ($this->timestamp !== NULL) {
      return $this->timestamp;
    }

    $item = $this->getParent();

    $date = $item->{($this->definition->getSetting('date source'))};
    $time = $item->{($this->definition->getSetting('time source'))};

    if (!empty($date)) {
      $timestamp = $date;

      if (!empty($time)) {
        $timestamp = $timestamp + $time;
      }

      $this->timestamp = $timestamp;
    }

    return $this->timestamp;
  }

  /**
   * {@inheritdoc}
   */
  public function setValue($value, $notify = TRUE) {
    $this->timestamp = $value;

    if ($notify && isset($this->parent)) {
      $this->parent->onChange($this->name);
    }
  }
}
+4 −1
Changes for src/Plugin/Field/FieldFormatter/OptionalDateCustomFormatter.php: 4 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2,11 +2,12 @@

namespace Drupal\optional_date\Plugin\Field\FieldFormatter;

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin for the 'Custom' formatter for optional_date fields.
 *
 * @FieldFormatter(
 *   id = "optional_date_custom",
 *   label = @Translation("Custom"),
@@ -16,6 +17,7 @@ use Drupal\Core\Form\FormStateInterface;
 * )
 */
class OptionalDateCustomFormatter extends OptionalDateFormatterBase {

  /**
   * {@inheritdoc}
   */
@@ -84,4 +86,5 @@ class OptionalDateCustomFormatter extends OptionalDateFormatterBase {

    return $form;
  }

}
+4 −1
Changes for src/Plugin/Field/FieldFormatter/OptionalDateFormatter.php: 4 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2,11 +2,12 @@

namespace Drupal\optional_date\Plugin\Field\FieldFormatter;

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin for the 'Default' formatter for optional_date fields.
 *
 * @FieldFormatter(
 *   id = "optional_date",
 *   label = @Translation("Default"),
@@ -16,6 +17,7 @@ use Drupal\Core\Form\FormStateInterface;
 * )
 */
class OptionalDateFormatter extends OptionalDateFormatterBase {

  /**
   * {@inheritdoc}
   */
@@ -50,4 +52,5 @@ class OptionalDateFormatter extends OptionalDateFormatterBase {

    return $form;
  }

}
Loading