Using Views Date Format SQL options on custom date fields

Created on 8 June 2018, about 7 years ago
Updated 28 February 2023, over 2 years ago

This is a great module, so thanks very much. We have a use-case for this module where we wanted to use it for our custom tables in our D8 database. They are using timestamps and so we weren't able to use aggregation as we wanted. However, we are using this with "Date" fields - I saw there's a few issues relating to the D7 version of this module, and have searched through the D8 issues, so sorry if this is a duplicate.

I've got it working for our data structure and will post a patch that hopefully is useful for somebody!

✨ Feature request
Status

Needs work

Version

3.0

Component

Code

Created by

šŸ‡¬šŸ‡§United Kingdom littlepixiez

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • šŸ‡¬šŸ‡§United Kingdom fonant

    Works for me with Drupal 9 if I patch with #3 then #18.

  • Issue was unassigned.
  • šŸ‡ŗšŸ‡øUnited States bluegeek9
  • šŸ‡®šŸ‡³India mukhtarm

    Hi I am using D9.5 and here is the patch that works for the custom date fields (I combined the #18 and #3 basically, but there were some tweaks needed as #18 was not applying cleanly). It works well to apply the custom SQL format to the custom field. Thanks

  • šŸ‡µšŸ‡¹Portugal lolgm

    I'm using D10.1 and patch #22 is working as expected, however, I'm getting the following warning:

    Trying to access array offset on value of type null views_date_format_sql.views.inc:34
    Undefined array key "field" views_date_format_sql.views.inc:34
    

    The attached patch fixes this problem.

  • šŸ‡®šŸ‡³India mukhtarm

    Thanks for the update @lolgm

  • šŸ‡µšŸ‡¹Portugal lolgm

    @MukhtarM I'm having a problem with the patch.
    It seems to me that something went wrong with the combination of patch #3 and #18.
    The code below is referencing a plugin that does not exist in the current patch.

    if (!empty($field['field']['id']) && $field['field']['id'] === 'date') {
      $field['field']['id'] = 'views_date_format_sql_date';
    }
    

    Could we remove this one?

  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    4 pass, 1 fail
  • šŸ‡®šŸ‡³India mukhtarm

    @lolgm Actually the patch didn't included the new plugin file (my bad :( ). Corrected it and included your changes also. thanks.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    4 pass, 1 fail
  • šŸ‡¬šŸ‡§United Kingdom fonant

    Patch in #26 applies cleanly to 3.x-dev and fixes the problem, Drupal 10. Thanks @MukhtarM!

  • First commit to issue fork.
  • First commit to issue fork.
  • šŸ‡ŗšŸ‡øUnited States loze Los Angeles

    loze → changed the visibility of the branch 8.x-3.x to hidden.

  • Pipeline finished with Success
    8 months ago
    Total: 425s
    #366197
  • šŸ‡ŗšŸ‡øUnited States loze Los Angeles

    Patch #26 was no longer applying to the the latest dev version, so I've created MR14 with the changes in #26

    here is a patch to use for composer

  • šŸ‡µšŸ‡¹Portugal lolgm

    I’m running a Drupal 10 site with this module installed.
    After upgrading the module from version 8.x-3.0-alpha4 to 8.x-3.0-alpha6 and replacing patch #26 with patch #32, views using date aggregation stopped working.

    It seems that patch #32 is not fully compatible with all use cases covered by patch #26.
    The attached patch is a reroll of patch #26.

  • šŸ‡¬šŸ‡§United Kingdom fonant

    Patch #33 working nicely here!. Thank you @lolgm for that :)

  • šŸ‡ŗšŸ‡øUnited States andileco

    Using patch #33 on Drupal 11.1.5. Works nicely if I use a custom formatter and (for my use case) set the format to "Y". If I try to use the "Default" formatter and select "HTML Year (2025)", I get the following in the dblog and the values do not show in my rendered view.

    Deprecated function: strtr(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\views\Plugin\views\query\MysqlDateSql->getDateFormat() (line 76 of /app/web/core/modules/views/src/Plugin/views/query/MysqlDateSql.php)
    
    #0 /app/web/core/includes/bootstrap.inc(108): _drupal_error_handler_real(8192, 'strtr(): Passin...', '/app/web/core/m...', 76)
    #1 [internal function]: _drupal_error_handler(8192, 'strtr(): Passin...', '/app/web/core/m...', 76)
    #2 /app/web/core/modules/views/src/Plugin/views/query/MysqlDateSql.php(76): strtr(NULL, Array)
    #3 /app/web/core/modules/views/src/Plugin/views/query/Sql.php(1890): Drupal\views\Plugin\views\query\MysqlDateSql->getDateFormat('storage__field_...', NULL)
    #4 /app/web/modules/contrib/views_date_format_sql/src/Plugin/views/field/ViewsDateFormatSqlField.php(217): Drupal\views\Plugin\views\query\Sql->getDateFormat('storage__field_...', NULL)
    #5 /app/web/core/modules/views/src/ViewExecutable.php(1433): Drupal\views_date_format_sql\Plugin\views\field\ViewsDateFormatSqlField->query(true)
    #6 /app/web/core/modules/views/src/ViewExecutable.php(1342): Drupal\views\ViewExecutable->_build('field')
    #7 /app/web/core/modules/views/src/ViewExecutable.php(1451): Drupal\views\ViewExecutable->build(NULL)
    #8 /app/web/core/modules/views/src/ViewExecutable.php(1514): Drupal\views\ViewExecutable->execute(NULL)
    #9 /app/web/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php(2493): Drupal\views\ViewExecutable->render()
    #10 /app/web/core/modules/views/src/ViewExecutable.php(1722): Drupal\views\Plugin\views\display\DisplayPluginBase->preview()
    #11 /app/web/core/modules/views_ui/src/ViewUI.php(615): Drupal\views\ViewExecutable->preview('attachment_1', Array)
    #12 /app/web/core/modules/views_ui/src/ViewPreviewForm.php(63): Drupal\views_ui\ViewUI->renderPreview('attachment_1', Array)
    #13 /app/web/core/lib/Drupal/Core/Entity/EntityForm.php(107): Drupal\views_ui\ViewPreviewForm->form(Array, Object(Drupal\Core\Form\FormState))
    #14 /app/web/core/modules/views_ui/src/ViewFormBase.php(42): Drupal\Core\Entity\EntityForm->buildForm(Array, Object(Drupal\Core\Form\FormState))
    #15 [internal function]: Drupal\views_ui\ViewFormBase->buildForm(Array, Object(Drupal\Core\Form\FormState), 'attachment_1')
    #16 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(528): call_user_func_array(Array, Array)
    #17 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(370): Drupal\Core\Form\FormBuilder->retrieveForm('view_preview_fo...', Object(Drupal\Core\Form\FormState))
    #18 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(625): Drupal\Core\Form\FormBuilder->rebuildForm('view_preview_fo...', Object(Drupal\Core\Form\FormState), Array)
    #19 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(321): Drupal\Core\Form\FormBuilder->processForm('view_preview_fo...', Array, Object(Drupal\Core\Form\FormState))
    #20 /app/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\views_ui\ViewPreviewForm), Object(Drupal\Core\Form\FormState))
    #21 /app/web/core/modules/layout_builder/src/Controller/LayoutBuilderHtmlEntityFormController.php(39): Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
    #22 [internal function]: Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
    #23 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
    #24 /app/web/core/lib/Drupal/Core/Render/Renderer.php(593): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #25 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
    #26 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
    #27 /app/vendor/symfony/http-kernel/HttpKernel.php(183): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #28 /app/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
    #29 /app/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #30 /app/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #31 /app/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #32 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(116): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #33 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(90): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #34 /app/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #35 /app/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #36 /app/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #37 /app/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #38 /app/web/core/lib/Drupal/Core/DrupalKernel.php(709): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #39 /app/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
    #40 {main}
    
Production build 0.71.5 2024