0

I am using the Views Database Connector module in drupal 8 to display the data from an external mssql database. I am running into issues with the date field. When I am asked to configure this field in the view, I am asked to choose a format for the date. However, when I go to view the page, I get this error (and stacktrace):

InvalidArgumentException: The timestamp must be numeric. in Drupal\Component\Datetime\DateTimePlus::createFromTimestamp() (line 172 of core/lib/Drupal/Component/Datetime/DateTimePlus.php).

Drupal\Core\Datetime\DateFormatter->format('2009-03-02', 'html_year', '', 'UTC', 'en') (Line: 323)

format_date('2009-03-02', 'html_year', '', NULL) (Line: 177)

Drupal\views\Plugin\views\field\Date->render(Object) (Line: 1144) Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender(Object) (Line: 224) template_preprocess_views_view_field(Array, 'views_view_field', Array) (Line: 287) Drupal\Core\Theme\ThemeManager->render('views_view_field', Array) (Line: 435) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)

I have not been able to figure out how to get the date to be converted to a timestamp so it can be formatted. I have tried a preprocessor but the template_preprocess_view_view_field always occurs first and the exception occurs before it gets to my preprocess function. Is there something particular about the date data type in mssql? Any suggestions how to get the date to not generate the exception?

  • I kind of cheated (and proved my case). I edited the DateTimePlus.php to NOT throw the error if not is_numeric (line 172). Instead, I simply converted the $timestamp to an actual timestamp: $timestamp = strtotime($timestamp); This indeed resolved my particular issue. My date field was not a timestamp, but needed to be converted to a timestamp in order to be formatted. Obviously, this is not the correct approach, but I was on the right track. The question now becomes, how do I do this 'legally'? Is there a way to override this? Does this become a ticket/feature request? – Chris Howell Oct 31 '17 at 18:31
0

Took me a while but I figured out what I needed to do - create a custom module with a plugin to override the Date.php plugin (which actually overrides the FieldPluginBase.php). Using the LastTimestamp.php plugin (which overrides the Date.php) from drupal's core comment module as a reference, I was able to piece together what I needed to do.

First step is when reading in the data from the tables (via the Views_Database_Connector module), I gave the drupal date field a different id rather than the default 'date' (eg "date_to_display"). This is the id used to trigger the call to the custom plugin rather than the Date.php plugin. While the VDC was the first step, actually giving the date field a different id was the final piece of the puzzle to make this work.

Next step was to create the custom module and then create the plugin to override Date.php and use the id in the annotation (this is important): @ViewsField("date_to_display"). You can read about this in Drupal's doc Annotations-Based Plugins in Views. In the custom plugin, override the render function. In my particular case, I simply just checked to see if the date field value was numeric (which seems to be required for this to not throw an exception), and if not, convert it to timestamp (new value = strtotime(value)). For me that did the trick. The top part of the created plugin looks like this:

<?php

namespace Drupal\my_module\Plugin\views\field;

use Drupal\views\Plugin\views\field\Date;
use Drupal\views\ResultRow;

/**
 * A handler to provide proper displays for dates.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("date_to_display")
 */
    class ViewsDateField extends Date {

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.