Correct code logic & remove duplicate code

Created on 9 February 2025, 2 months ago

Problem/Motivation

I notice in your NodeAvailabilityService service code, you fetching the field value early then checking about node empty

Proposed resolution

$start_date = $node->get($field_date)->value ? strtotime($node->get($field_date)->value) : 0; // Remove this line before check about node found
    $expire_date = $node->get($field_date)->end_value ? strtotime($node->get($field_date)->end_value) : 0; // Remove this line before check about node found
    // Return early if the node is not found or not published.
    if (!$node || !$node->isPublished()) {
      return [
        'label' => t('Unavailable'),
        'label_class' => 'unavailable-label',
      ];
    }
    // Get the current time.
    $current_date = $this->time->getCurrentTime();
    // Get the start and end dates from the field.
    $start_date = $node->get($field_date)->value ? strtotime($node->get($field_date)->value) : 0;
    $expire_date = $node->get($field_date)->end_value ? strtotime($node->get($field_date)->end_value) : 0;
🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇮🇳India Ujjval Kumar Jha

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024