0

I am using the following code to set create a field programmtically and I am trying to set the cardinality as unlimited. But I am getting the error and I cannot add any values.

Symfony\Component\Validator\Exception\MissingOptionsException: Either option "min" or "max" must be given for constraint Symfony\Component\Validator\Constraints\Count in Symfony\Component\Validator\Constraints\Count->__construct() (line 51 of /var/www/heritage/vendor/symfony/validator/Constraints/Count.php

Here is the function which I am using to create field: cardinality being set as $cardinality = [-1];

    /**
 * Custom function to add a field to an entity.
 *
 * @param string $bundle
 *   Name of the entity bundle.
 * @param object $entity_type
 *   Type of entity ie. whether node/taxonomy/user.
 * @param array $labels
 *   Array of field labels.
 * @param string $machine_names
 *   Array of Machine Names of the field.
 * @param array $field_types
 *   Array of Field Types to be added.
 * @param array $format_tmp_array
 *   Array of field formats
 * @param array $cardinality
 *   Array of field cardinalities
 */
function add_other_fields($bundle, $entity_type, array $labels, $machine_names, array $field_types, rray $format_tmp_array,array $cardinality) {
  for ($i = 0; $i < count($field_types); $i++) {
    $fieldname = strtolower($machine_names[$i]);
    if ($field_types[$i] == 'entity_reference') {
      $handler = 'default:taxonomy_term';
      $handler_settings = ['target_bundles' => [$bundle]];
      $settings_create = ['target_type' => 'taxonomy_term'];
      $settings_config = ['handler' => $handler, 'handler_settings' => $handler_settings];
    }
    elseif ($field_types[$i] == 'file' && $format_tmp_array[$i] == 'audio') {
      $handler = 'default';
      $handler_settings = [];
      $settings_create = [];
      $settings_config = [
        'handler' => $handler,
        'handler_settings' => $handler_settings,
        'file_extensions' => 'mp3',
        'file_directory' => $bundle,
      ];
    }
    elseif ($field_types[$i] == 'file' && $format_tmp_array[$i] == 'video') {
      $handler = 'default';
      $handler_settings = [];
      $settings_create = [];
      $settings_config = [
        'handler' => $handler,
        'handler_settings' => $handler_settings,
        'file_extensions' => 'mp4',
        'file_directory' => $bundle,
      ];
    }

    else {
      $handler = 'default';
      $handler_settings = [];
      $settings_create = [];
      $settings_config = [
        'handler' => $handler,
        'handler_settings' => $handler_settings,
      ];
    }
  }
    if (empty($fieldStorageMicrositesfield = FieldStorageConfig::loadByName($entity_type, $fieldname))) {
      $fieldStorage = FieldStorageConfig::create(
      [
        'field_name'             => $fieldname,
        'entity_type'            => $entity_type,
        'type'                   => $field_types[$i],
        'translatable'           => TRUE,
        'persist_with_no_fields' => FALSE,
        'cardinality'             => $cardinality[$i],
        'settings'                 => $settings_create,
      ]
      );
      $fieldStorageSaved = $fieldStorage->save();
    }
    $all_bundle_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type, $bundle);
    if (!isset($all_bundle_fields[$fieldname])) {
      $field = FieldConfig::create(
                        [
                          'entity_type' => $entity_type,
                          'field_name' => $fieldname,
                          'bundle' => $bundle,
                          'label' => $labels[$i],
                          'settings' => $settings_config,
                        ]
      );
      $field->save();

      // Make the field available in the entity creation form.
      entity_get_form_display($entity_type, $bundle, 'default')
        ->setComponent(
      $fieldname
      )
        ->save();

      // Assign display settings for the 'default' and 'teaser' view modes.
      entity_get_display($entity_type, $bundle, 'default')
        ->setComponent($fieldname)
        ->save();
    }
    else {
      $field_info = FieldConfig::loadByName($entity_type, $bundle, $fieldname);
      $field_info->setLabel($labels[$i]);
      $field_info->save();
    }


  $message = t("Fields added successfully");
  return $message;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.