Skip to content

Warning: Attempt to read property "name" on array in paragraphs_bundle_title_callback() (line 1210 of paragraphs.module). #228

@alanmels

Description

@alanmels

The warning persists during uninstalling a custom module that uses paragraphs, because by that time the module's hook_menu_alter no longer runs, and Backdrop calls Paragraphs’ own paragraphs_bundle_title_callback(), which assumes $bundle is always an object. Would be nice if Paragraphs’ callback accepted arrays (even with multiple objects), so the code would look something like:

function paragraphs_bundle_title_callback($bundle) {
  // Accept arrays (sometimes contain one or more bundle objects during menu rebuild).
  if (is_array($bundle)) {
    foreach ($bundle as $v) {
      if (is_object($v)) {
        $bundle = $v;
        break;
      }
      // Handle nested arrays defensively.
      if (is_array($v)) {
        foreach ($v as $v2) {
          if (is_object($v2)) { $bundle = $v2; break 2; }
        }
      }
    }
  }

  $name = '';
  if (is_object($bundle)) {
    // Prefer label, then name, then bundle machine name.
    if (!empty($bundle->label)) {
      $name = $bundle->label;
    }
    elseif (!empty($bundle->name)) {
      $name = $bundle->name;
    }
    elseif (!empty($bundle->bundle)) {
      $name = $bundle->bundle;
    }
  }
  elseif (is_string($bundle)) {
    $name = $bundle;
  }

  if ($name === '') {
    $name = t('Paragraph type');
  }
  return t('@name', array('@name' => $name));
}

Until this fixed, we are temporarily using this tiny module:

para_title_fix.info

name = Paragraphs title fix
description = Makes Paragraphs bundle title callback tolerate array/object during menu rebuilds.
type = module
core = 1.x
package = Custom

para_title_fix.module

<?php
<?php

/**
 * Implements hook_menu_alter().
 * Replace Paragraphs' bundle title callback with a robust version.
 */
function para_title_fix_menu_alter(&$items) {
  foreach ($items as &$def) {
    if (!empty($def['title callback']) && $def['title callback'] === 'paragraphs_bundle_title_callback') {
      $def['title callback'] = 'para_title_fix_paragraphs_bundle_title_callback';
    }
  }
}

/**
 * Safe title callback for Paragraphs bundle pages.
 * Accepts an object or an array (even with multiple objects).
 */
function para_title_fix_paragraphs_bundle_title_callback($bundle) {
  // Normalize arrays to the first object found (also handle nested arrays).
  if (is_array($bundle)) {
    $bundle = _para_title_fix_first_object($bundle);
  }

  $name = '';
  if (is_object($bundle)) {
    $name = !empty($bundle->label) ? $bundle->label
          : (!empty($bundle->name) ? $bundle->name
          : (!empty($bundle->bundle) ? $bundle->bundle : ''));
  } elseif (is_string($bundle)) {
    $name = $bundle;
  }

  if ($name === '') {
    $name = t('Paragraph type');
  }
  return t('@name', array('@name' => $name));
}

/**
 * Find the first object within a (possibly nested) array.
 */
function _para_title_fix_first_object($value) {
  foreach ($value as $v) {
    if (is_object($v)) {
      return $v;
    }
    if (is_array($v)) {
      $o = _para_title_fix_first_object($v);
      if (is_object($o)) {
        return $o;
      }
    }
  }
  return (object) array();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions