Error on "Room Search" in lack of cover images

Created on 29 April 2024, 7 months ago
Updated 7 May 2024, 7 months ago

Problem/Motivation

TypeError: Cannot access offset of type string on string in Drupal\bee_hotel\Controller\SearchResult->produceResult() (line 210 of /app/web/modules/contrib/bee_hotel/src/Controller/SearchResult.php).

Steps to reproduce

In case one room have no cover image, the site crash on "Room Search" /u

Proposed resolution

Cover image should be made mandatory or better error handling for SearchResult.php

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs review

Version

2.21

Component

Code

Created by

🇷🇴Romania alexberendei

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

Comments & Activities

  • Issue created by @alexberendei
  • Assigned to praveen rani
  • Issue was unassigned.
  • Assigned to Rajan Kumar@2026
  • 🇮🇳India Rajan Kumar@2026

    Hello @alexberendei

    i installed this module but inside src folder where controller folder is not available pls provide more details.

    in info file d10 compatible missing hare i created a patch.

  • Issue was unassigned.
  • 🇷🇴Romania alexberendei

    on the file /bee_hotel/src/Controller/SearchResult.php at line 210:

    $data['destination']['img'] = Link::createFromRoute($beeHotelUnit['cover_image']['markup'], 'entity.node.canonical', [
           'node' => $beeHotelUnit['node']->Id(),
            'v' => $data['v_param'],
    ]);
    

    In case there is one node with no image uploaded (cover_image), the site crashes when you request a search on /RoomSearch

    I propose this code:

    // Check if cover image exists before accessing it
    if (isset($beeHotelUnit['cover_image']) && is_array($beeHotelUnit['cover_image']) && isset($beeHotelUnit['cover_image']['markup'])) {
        $data['destination']['img'] = Link::createFromRoute($beeHotelUnit['cover_image']['markup'], 'entity.node.canonical', [
           'node' => $beeHotelUnit['node']->Id(),
           'v' => $data['v_param'],
        ]);
    } else {
        // Handle case where cover image is not available
        $data['destination']['img'] = t('No Image');
    }
    
  • Status changed to Needs review 7 months ago
  • 🇮🇹Italy afagioli Rome

    Thanks for pointing that out!

    Next release will have something like

    /**
    
    ...
    use Drupal\image\Entity\ImageStyle;
    
    /**
     * Implements hook_requirements().
     */
    function bee_hotel_requirements($phase) {
    
      $requirements = [];
    
      /*
       * Check image styles
       */
    
      // Required image styles.
      $data['required']['styles'] = [
        "cover_image_teaser",
        "thumbnail",
      ];
    
      $styles = ImageStyle::loadMultiple();
    
      foreach ($data['required']['styles'] as $required_style) {
        $key = 'bee_hotel_style_' . $required_style;
        $missings_styles[$required_style] = FALSE;
        $requirements[$key] = [
          'title' => t('Required image style'),
          'description' => t('@style not fund. Please <a href="/admin/config/media/image-styles">check or create image style @style', ['@style' => $required_style]),
          'severity' => REQUIREMENT_ERROR,
        ];
        foreach  (ImageStyle::loadMultiple() as $id => $object)  {
          if ($required_style ==  $id ) {
            unset($requirements[$key]);
          }
        }
      }
    
      return $requirements;
    }

    inside bee_hotel.install, reporting missing requirements, as styles.

    To be noted also required styles are created by the samplehotel_beehotel module

Production build 0.71.5 2024