Skip to content

Instantly share code, notes, and snippets.

@woprrr
Created October 4, 2017 07:40
Show Gist options
  • Save woprrr/12c8f1f503a55d33236c8b1659320050 to your computer and use it in GitHub Desktop.
Save woprrr/12c8f1f503a55d33236c8b1659320050 to your computer and use it in GitHub Desktop.
Drupal 8 Dynamic field cardinality
$field_updated = &$form['YOUR_FIELD_NAME'];
$field_state = \Drupal\Core\Field\WidgetBase::getWidgetState($form['#parents'], $field_updated['widget']['#field_name'], $form_state);
// You can change number of item to control the number of items we can change it with another field value into $form_state.
if ($field_state['items_count'] >= 2) {
$field_updated['widget']['add_more']['#access'] = FALSE;
}
$form['YOUR_FIELD_NAME'] = $field_updated;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment