本文整理汇总了PHP中Drupal\field\Entity\FieldStorageConfig::getCardinality方法的典型用法代码示例。如果您正苦于以下问题:PHP FieldStorageConfig::getCardinality方法的具体用法?PHP FieldStorageConfig::getCardinality怎么用?PHP FieldStorageConfig::getCardinality使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\field\Entity\FieldStorageConfig
的用法示例。
在下文中一共展示了FieldStorageConfig::getCardinality方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testTranslatableFieldSaveLoad
/**
* Test translatable fields storage/retrieval.
*/
function testTranslatableFieldSaveLoad()
{
// Enable field translations for nodes.
field_test_entity_info_translatable('node', TRUE);
$entity_type = \Drupal::entityManager()->getDefinition('node');
$this->assertTrue($entity_type->isTranslatable(), 'Nodes are translatable.');
// Prepare the field translations.
$entity_type_id = 'entity_test';
field_test_entity_info_translatable($entity_type_id, TRUE);
$entity = entity_create($entity_type_id, array('type' => $this->field->bundle));
$field_translations = array();
$available_langcodes = array_keys($this->container->get('language_manager')->getLanguages());
$entity->langcode->value = reset($available_langcodes);
foreach ($available_langcodes as $langcode) {
$field_translations[$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
$entity->getTranslation($langcode)->{$this->field_name}->setValue($field_translations[$langcode]);
}
// Save and reload the field translations.
$entity = $this->entitySaveReload($entity);
// Check if the correct values were saved/loaded.
foreach ($field_translations as $langcode => $items) {
$result = TRUE;
foreach ($items as $delta => $item) {
$result = $result && $item['value'] == $entity->getTranslation($langcode)->{$this->field_name}[$delta]->value;
}
$this->assertTrue($result, format_string('%language translation correctly handled.', array('%language' => $langcode)));
}
// Test default values.
$field_name_default = drupal_strtolower($this->randomMachineName() . '_field_name');
$field_storage_definition = $this->field_storage_definition;
$field_storage_definition['field_name'] = $field_name_default;
$field_storage = entity_create('field_storage_config', $field_storage_definition);
$field_storage->save();
$field_definition = $this->field_definition;
$field_definition['field_storage'] = $field_storage;
$field_definition['default_value'] = array(array('value' => rand(1, 127)));
$field = entity_create('field_config', $field_definition);
$field->save();
$translation_langcodes = array_slice($available_langcodes, 0, 2);
asort($translation_langcodes);
$translation_langcodes = array_values($translation_langcodes);
$values = array('type' => $field->bundle, 'langcode' => $translation_langcodes[0]);
$entity = entity_create($entity_type_id, $values);
foreach ($translation_langcodes as $langcode) {
$values[$this->field_name][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
$entity->getTranslation($langcode, FALSE)->{$this->field_name}->setValue($values[$this->field_name][$langcode]);
}
$field_langcodes = array_keys($entity->getTranslationLanguages());
sort($field_langcodes);
$this->assertEqual($translation_langcodes, $field_langcodes, 'Missing translations did not get a default value.');
// @todo Test every translation once the Entity Translation API allows for
// multilingual defaults.
$langcode = $entity->language()->getId();
$this->assertEqual($entity->getTranslation($langcode)->{$field_name_default}->getValue(), $field->default_value, format_string('Default value correctly populated for language %language.', array('%language' => $langcode)));
// Check that explicit empty values are not overridden with default values.
foreach (array(NULL, array()) as $empty_items) {
$values = array('type' => $field->bundle, 'langcode' => $translation_langcodes[0]);
$entity = entity_create($entity_type_id, $values);
foreach ($translation_langcodes as $langcode) {
$values[$this->field_name][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
$entity->getTranslation($langcode)->{$this->field_name}->setValue($values[$this->field_name][$langcode]);
$entity->getTranslation($langcode)->{$field_name_default}->setValue($empty_items);
$values[$field_name_default][$langcode] = $empty_items;
}
foreach ($entity->getTranslationLanguages() as $langcode => $language) {
$this->assertEqual($entity->getTranslation($langcode)->{$field_name_default}->getValue(), $empty_items, format_string('Empty value correctly populated for language %language.', array('%language' => $langcode)));
}
}
}