1

Is there any way to get the number of paragraphs' repeating limit for the entity's field which those paragraphs depends on?

I'm writing a code which creates paragraphs according to the data taken from a CSV data and and insert their target ID (and target revision ID) into user fields programmatically.

The repeating limits of paragraphs for each user field differs. Some user fields allow paragraphs to repeat only a few times and others expect them to repeat 10 times.

So if my guess was correct it would be useful if I can get the number when collecting data from CSV and that's why I'm asking.

closed as too broad by Pierre.Vriens, mradcliffe, DRUPWAY, kiamlaluno Dec 25 '18 at 9:20

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Probably you find your solution here: drupal.stackexchange.com/questions/191402/… , except if it's dynamically set with validation criteria on form submit. So you get the field settings, and you search for the right settings, it should be there. – golddragon007 Dec 19 '18 at 10:58
1

Thanks for the comments, I figured out a way to get the number(cardinality). Hope it helps someone.

(array) $user_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('user', 'user');

foreach ($user_fields as $user_field_name => $user_field_definition) {
    (array) $cardinality = $user_field_definition->get('fieldStorage')->get('cardinality');
}
  • 1
    Instead of the generic get() methods, you really should be using the dedicated getters. This is because get('fieldStorage') may return NULL if the fieldStorage property has not been initialized yet. This always works: $cardinality = $user_field_definition->getFieldStorageDefinition()->getCardinality(); – marcvangend Apr 4 at 12:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.