Problem/Motivation

The Select handler is throwing a notice (might happen with Number handler as well) because the field_cardinality index does not exist in $options.
Undefined index: field_cardinality in Drupal\conditional_fields\Plugin\conditional_fields\handler\Select->widgetCase() (line 82 of modules/contrib/conditional_fields/src/Plugin/conditional_fields/handler/Select.php).

Steps to reproduce

Create a condition on a select field.

Proposed resolution

Assuming that a field without the field_cardinality index is treated as cardinality = 1;

$single = empty($options['field_cardinality']) || $options['field_cardinality'] == 1;
if (!empty($options['value_form'][0][$key_column]) && $single) {
... }

Comments

tklawsuc created an issue. See original summary.

hmendes’s picture

Hello!
I was unable to reproduce your error. Could you provide more steps to reproduce the problem?

maxwellkeeble’s picture

I have the same issue on a form, but in my case the "controlled by" field is an entity reference select element (which is set up to allow one value only) that references a custom entity (not a node).

Could it be that the "states_handler_entity_reference_autocomplete" plugins is hard-wired just for nodes, even though it indicates it should be for generic entities?

The error is:

Notice: Undefined index: field_cardinality in Drupal\conditional_fields\Plugin\conditional_fields\handler\EntityReference->statesHandler() (line 31 of modules/contrib/conditional_fields/src/Plugin/conditional_fields/handler/EntityReference.php).
Drupal\conditional_fields\Plugin\conditional_fields\handler\EntityReference->statesHandler(Array, Array, Array) (Line: 216)
Drupal\conditional_fields\ConditionalFieldsFormHelper->getState('group_role_id', Array, Array) (Line: 122)
Drupal\conditional_fields\ConditionalFieldsFormHelper->processDependeeFields(Array) (Line: 77)
Drupal\conditional_fields\ConditionalFieldsFormHelper->processDependentFields() (Line: 51)
Drupal\conditional_fields\ConditionalFieldsFormHelper->afterBuild(Array, Object) (Line: 406)
conditional_fields_form_after_build(Array, Object)
etc.
drclaw’s picture

Title: undefined index field_cardinality » Undefined index field_cardinality when dependee field is a base field definition
Version: 8.x-1.x-dev » 4.x-dev
Status: Active » Needs review
StatusFileSize
new936 bytes

So this is a bit of a weird one and requires some specific conditions to trigger the error:

  • The dependee/control field must be a BaseFieldDefinition (e.g. status, promoted, uid etc. on Nodes)
  • The dependee/control field must use one of the following conditional field handlers: EntityReference, EntityReferenceTags, Number, Select
  • The dependee/control field has the 'value' condition

You can recreate the issue on any node type by setting the control field to "Authored By (uid)" and setting the "when the control field" dropdown to "has value...". Then load the node add/edit form and you should see it.

The root cause of the issue is actually the first item in the list. The field_cardinality value is only set for FieldStorageConfig fields (e.g. fields created via "Manage Fields"), but not for BaseFieldDefinitions. The fix is simple enough: we just need to also check base field definitions for the dependee field. Patch attached!

Status: Needs review » Needs work

The last submitted patch, 4: conditional_fields--base_field_definition_cardinality--3200178-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

nagy.balint’s picture

On one of our sites where content entity builder was used to create a base field on an entity, we got the undefined index as in the title.

The patch #4 fixes the issue.

joaopauloc.dev’s picture

Patch #4 worked for me.

My setup.

Drupal 11.2
Conditional fields 4@alpha
Address 2.0.4.

My address field(country, state and location visible) is visible when a certain condition is true, but after i added this condition i could see that error on form of my custom entity. Also, the address field is inside a field group.

With the patch the warning disapear.