Problem/Motivation
So I wanted to have an optional text field that links to a PDF via an Entity reference field. If the PDF media file is empty than this field should be emptied. It works when the PDF field is empty and it empties the text field as required but when I try to save a node that already has a PDF media file attached it fails to save the node with the following error.
The website encountered an unexpected error. Try again later.
Error: Cannot use object of type Drupal\Core\StringTranslation\TranslatableMarkup as array in Drupal\conditional_fields\ConditionalFieldsFormHelper::evaluateDependency() (line 936 of modules/contrib/conditional_fields/src/ConditionalFieldsFormHelper.php).
Steps to reproduce
Create a content type with a text field and a node reference to a media object.
Create a dependency where the text_field is emptied if the media object is empty.
Edit a node that has some a value in the text field and there isn't a media attached and it works.
Edit a node that has a media object and get the error above.
Proposed resolution
The code should be able to check if a node reference to a media object exists without crashing.
This is the code that is failing for the following function
protected static function evaluateDependency($context, $values, array $options) {
if ($options['condition'] === 'empty') {
$values = (isset($values[0]['value'])) ? $values[0]['value'] : $values;
$values = ($values === '_none') ? '' : $values;
return (empty($values)) ? TRUE : FALSE;
}
It might be necessary to check whether an object or node reference exists in values before it starts to treat it like an array as that seems to trigger the error. Perhaps checking if is_object($values) and returning FALSE if it is would be a way to fix this issue. This seems to work but I'm not familiar enough with the codebase to propose a MR.
Comments