Fatal error when saving new topic

Created on 9 November 2023, about 1 year ago
Updated 29 March 2024, 9 months ago

Problem/Motivation

A fatal error is triggered when creating new content without manually set creation date.

Steps to reproduce

  • Go to one of the groups.
  • Create new topic
  • Do not fill in the creation date
  • Save the new topic

Topic is not saved due to fatal error:

Error: Call to a member function getTimestamp() on null in _social_core_created_date_validation() (line 1095 of profiles/contrib/open_social/modules/social_features/social_core/social_core.module).

Proposed resolution

The code within the _social_core_created_date_validation() function does check if the form has any value but not of an actual date is set. This check should be added.

function _social_core_created_date_validation(array $form, FormStateInterface $form_state): void {
  $created = $form_state->getValue('created');
  if (!$created) {
    return;
  }

  $date = end($created);
  $date = $date['value'];
  if (!$date instanceof DrupalDateTime) {
    return;
  }

  if ($date->getTimestamp() > time()) {
    $form_state->setErrorByName('created', t('The created date cannot be in the future.'));
  }
}

Remaining tasks

  • Update code.
🐛 Bug report
Status

Needs work

Version

12.0

Component

Code (back-end)

Created by

🇧🇪Belgium zero2one

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024