本文整理汇总了PHP中Drupal\Core\Field\FieldDefinitionInterface::getFieldStorageDefinition方法的典型用法代码示例。如果您正苦于以下问题:PHP FieldDefinitionInterface::getFieldStorageDefinition方法的具体用法?PHP FieldDefinitionInterface::getFieldStorageDefinition怎么用?PHP FieldDefinitionInterface::getFieldStorageDefinition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Field\FieldDefinitionInterface
的用法示例。
在下文中一共展示了FieldDefinitionInterface::getFieldStorageDefinition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: view
/**
* {@inheritdoc}
*/
public function view(FieldItemListInterface $items, $langcode = NULL)
{
// Default the language to the current content language.
if (empty($langcode)) {
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
}
$elements = $this->viewElements($items, $langcode);
// If there are actual renderable children, use #theme => field, otherwise,
// let access cacheability metadata pass through for correct bubbling.
if (Element::children($elements)) {
$entity = $items->getEntity();
$entity_type = $entity->getEntityTypeId();
$field_name = $this->fieldDefinition->getName();
$info = array('#theme' => 'field', '#title' => $this->fieldDefinition->getLabel(), '#label_display' => $this->label, '#view_mode' => $this->viewMode, '#language' => $items->getLangcode(), '#field_name' => $field_name, '#field_type' => $this->fieldDefinition->getType(), '#field_translatable' => $this->fieldDefinition->isTranslatable(), '#entity_type' => $entity_type, '#bundle' => $entity->bundle(), '#object' => $entity, '#items' => $items, '#formatter' => $this->getPluginId(), '#is_multiple' => $this->fieldDefinition->getFieldStorageDefinition()->isMultiple());
$elements = array_merge($info, $elements);
}
return $elements;
}
示例2: formMultipleElements
/**
* Special handling to create form elements for multiple values.
*
* Handles generic features for multiple fields:
* - number of widgets
* - AHAH-'add more' button
* - table display and drag-n-drop value reordering
*/
protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state)
{
$field_name = $this->fieldDefinition->getName();
$cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
$parents = $form['#parents'];
// Determine the number of widgets to display.
switch ($cardinality) {
case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED:
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$max = $field_state['items_count'];
$is_multiple = TRUE;
break;
default:
$max = $cardinality - 1;
$is_multiple = $cardinality > 1;
break;
}
$title = $this->fieldDefinition->getLabel();
$description = FieldFilteredMarkup::create(\Drupal::token()->replace($this->fieldDefinition->getDescription()));
$elements = array();
for ($delta = 0; $delta <= $max; $delta++) {
// Add a new empty item if it doesn't exist yet at this delta.
if (!isset($items[$delta])) {
$items->appendItem();
}
// For multiple fields, title and description are handled by the wrapping
// table.
if ($is_multiple) {
$element = ['#title' => $this->t('@title (value @number)', ['@title' => $title, '@number' => $delta + 1]), '#title_display' => 'invisible', '#description' => ''];
} else {
$element = ['#title' => $title, '#title_display' => 'before', '#description' => $description];
}
$element = $this->formSingleElement($items, $delta, $element, $form, $form_state);
if ($element) {
// Input field for the delta (drag-n-drop reordering).
if ($is_multiple) {
// We name the element '_weight' to avoid clashing with elements
// defined by widget.
$element['_weight'] = array('#type' => 'weight', '#title' => $this->t('Weight for row @number', array('@number' => $delta + 1)), '#title_display' => 'invisible', '#delta' => $max, '#default_value' => $items[$delta]->_weight ?: $delta, '#weight' => 100);
}
$elements[$delta] = $element;
}
}
if ($elements) {
$elements += array('#theme' => 'field_multiple_value_form', '#field_name' => $field_name, '#cardinality' => $cardinality, '#cardinality_multiple' => $this->fieldDefinition->getFieldStorageDefinition()->isMultiple(), '#required' => $this->fieldDefinition->isRequired(), '#title' => $title, '#description' => $description, '#max_delta' => $max);
// Add 'add more' button, if not working with a programmed form.
if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && !$form_state->isProgrammed()) {
$id_prefix = implode('-', array_merge($parents, array($field_name)));
$wrapper_id = Html::getUniqueId($id_prefix . '-add-more-wrapper');
$elements['#prefix'] = '<div id="' . $wrapper_id . '">';
$elements['#suffix'] = '</div>';
$elements['add_more'] = array('#type' => 'submit', '#name' => strtr($id_prefix, '-', '_') . '_add_more', '#value' => t('Add another item'), '#attributes' => array('class' => array('field-add-more-submit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name))), '#submit' => array(array(get_class($this), 'addMoreSubmit')), '#ajax' => array('callback' => array(get_class($this), 'addMoreAjax'), 'wrapper' => $wrapper_id, 'effect' => 'fade'));
}
}
return $elements;
}
示例3: createPayment
/**
* {@inheritdoc}
*/
public function createPayment(FieldDefinitionInterface $field_definition)
{
/** @var \Drupal\payment\Entity\PaymentInterface $payment */
$payment = $this->entityManager->getStorage('payment')->create(['bundle' => 'payment_reference']);
/** @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference $payment_type */
$payment_type = $payment->getPaymentType();
$payment_type->setEntityTypeId($field_definition->getFieldStorageDefinition()->getTargetEntityTypeId());
$payment_type->setBundle($field_definition->getTargetBundle());
$payment_type->setFieldName($field_definition->getName());
$payment->setCurrencyCode($field_definition->getSetting('currency_code'));
foreach ($field_definition->getSetting('line_items_data') as $line_item_data) {
$line_item = $this->paymentLineItemManager->createInstance($line_item_data['plugin_id'], $line_item_data['plugin_configuration']);
$payment->setLineItem($line_item);
}
return $payment;
}
示例4: getOptions
public function getOptions(FieldDefinitionInterface $field, $component)
{
$fs = $field->getFieldStorageDefinition()->getSettings();
$options = $fs[$component . '_options'];
foreach ($options as $index => $opt) {
if (preg_match('/^\\[vocabulary:([0-9a-z\\_]{1,})\\]/', trim($opt), $matches)) {
unset($options[$index]);
if ($this->termStorage && $this->vocabularyStorage) {
$vocabulary = $this->vocabularyStorage->load($matches[1]);
if ($vocabulary) {
$max_length = isset($fs['max_length'][$component]) ? $fs['max_length'][$component] : 255;
foreach ($this->termStorage->loadTree($vocabulary->id()) as $term) {
if (Unicode::strlen($term->name) <= $max_length) {
$options[] = $term->name;
}
}
}
}
}
}
// Options could come from multiple sources, filter duplicates.
$options = array_unique($options);
if (isset($fs['sort_options']) && !empty($fs['sort_options'][$component])) {
natcasesort($options);
}
$default = FALSE;
foreach ($options as $index => $opt) {
if (strpos($opt, '--') === 0) {
unset($options[$index]);
$default = trim(Unicode::substr($opt, 2));
}
}
$options = array_map('trim', $options);
$options = array_combine($options, $options);
if ($default !== FALSE) {
$options = array('' => $default) + $options;
}
return $options;
}
示例5: isApplicable
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition)
{
// This formatter is only available for entity types that have a view
// builder.
$target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
return \Drupal::entityManager()->getDefinition($target_type)->hasViewBuilderClass();
}
示例6: onDependencyRemoval
/**
* {@inheritdoc}
*/
public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies)
{
$changed = FALSE;
if (!empty($field_definition->default_value)) {
$target_entity_type = \Drupal::entityManager()->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type'));
foreach ($field_definition->default_value as $key => $default_value) {
if (is_array($default_value) && isset($default_value['target_uuid'])) {
$entity = \Drupal::entityManager()->loadEntityByUuid($target_entity_type->id(), $default_value['target_uuid']);
// @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue()
if ($entity && isset($dependencies[$entity->getConfigDependencyKey()][$entity->getConfigDependencyName()])) {
unset($field_definition->default_value[$key]);
$changed = TRUE;
}
}
}
}
return $changed;
}
示例7: getMatches
/**
* Get matches for the autocompletion of name components.
*
* @param FieldDefinitionInterface $field
* The field definition.
*
* @param $target
* The name field component.
*
* @param string $string
* The string to match for the name field component.
*
* @return array
* An array containing the matching values.
*/
public function getMatches(FieldDefinitionInterface $field, $target, $string)
{
$matches = array();
$limit = 10;
if (empty($string)) {
return $matches;
}
$settings = $field->getFieldStorageDefinition()->getSettings();
foreach ($this->allComponents as $component) {
if (!isset($settings['autocomplete_source'][$component])) {
$settings['autocomplete_source'][$component] = array();
}
$settings['autocomplete_source'][$component] = array_filter($settings['autocomplete_source'][$component]);
}
$action = array();
switch ($target) {
case 'name':
$action['components'] = $this->mapAssoc(array('given', 'middle', 'family'));
break;
case 'name-all':
$action['components'] = $this->mapAssoc($this->allComponents);
break;
case 'title':
case 'given':
case 'middle':
case 'family':
case 'credentials':
case 'generational':
$action['components'] = array($target => $target);
break;
default:
$action['components'] = array();
foreach (explode('-', $target) as $component) {
if (in_array($component, array('title', 'given', 'middle', 'family', 'credentials', 'generational'))) {
$action['components'][$component] = $component;
}
}
break;
}
$action['source'] = array('title' => array(), 'generational' => array());
$action['separater'] = '';
foreach ($action['components'] as $component) {
if (empty($settings['autocomplete_source'][$component])) {
unset($action['components'][$component]);
} else {
$sep = (string) $settings['autocomplete_separator'][$component];
if (empty($sep)) {
$sep = ' ';
}
for ($i = 0; $i < count($sep); $i++) {
if (strpos($action['separater'], $sep[$i]) === FALSE) {
$action['separater'] .= $sep[$i];
}
}
$found_source = FALSE;
foreach ((array) $settings['autocomplete_source'][$component] as $src) {
if ($src == 'data' && !$field) {
continue;
}
if ($src == 'title' || $src == 'generational') {
if (!$field || $component != $src) {
continue;
}
}
$found_source = TRUE;
$action['source'][$src][] = $component;
}
if (!$found_source) {
unset($action['components'][$component]);
}
}
}
/**
* @todo: preg_split fails with a notice if $action['separater'] == ' '.
*/
@($pieces = preg_split('/[' . preg_quote($action['separater']) . ']+/', $string));
// We should have nice clean parameters to query.
if (!empty($pieces) && !empty($action['components'])) {
$test_string = Unicode::strtolower(array_pop($pieces));
$base_string = Unicode::substr($string, 0, Unicode::strlen($string) - Unicode::strlen($test_string));
if ($limit > 0 && count($action['source']['title'])) {
$options = $this->optionsProvider->getOptions($field, 'title');
foreach ($options as $key => $option) {
if (strpos(Unicode::strtolower($key), $test_string) === 0 || strpos(Unicode::strtolower($option), $test_string) === 0) {
$matches[$base_string . $key] = $key;
//.........这里部分代码省略.........
示例8: generateSampleValue
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition)
{
$manager = \Drupal::service('plugin.manager.entity_reference_selection');
// Instead of calling $manager->getSelectionHandler($field_definition)
// replicate the behavior to be able to override the sorting settings.
$options = array('target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'), 'handler' => $field_definition->getSetting('handler'), 'handler_settings' => $field_definition->getSetting('handler_settings') ?: array(), 'entity' => NULL);
$entity_type = \Drupal::entityManager()->getDefinition($options['target_type']);
$options['handler_settings']['sort'] = ['field' => $entity_type->getKey('id'), 'direction' => 'DESC'];
$selection_handler = $manager->getInstance($options);
// Select a random number of references between the last 50 referenceable
// entities created.
if ($referenceable = $selection_handler->getReferenceableEntities(NULL, 'CONTAINS', 50)) {
$group = array_rand($referenceable);
$values['target_id'] = array_rand($referenceable[$group]);
return $values;
}
}
示例9: isApplicable
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition)
{
return $field_definition->getFieldStorageDefinition()->getSetting('target_type') == 'user';
}
示例10: getSelectionHandler
/**
* {@inheritdoc}
*/
public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL)
{
$options = array('target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'), 'handler' => $field_definition->getSetting('handler'), 'handler_settings' => $field_definition->getSetting('handler_settings') ?: array(), 'entity' => $entity);
return $this->getInstance($options);
}
示例11: calculateDependencies
/**
* {@inheritdoc}
*/
public static function calculateDependencies(FieldDefinitionInterface $field_definition)
{
$dependencies = [];
if (is_array($field_definition->getDefaultValueLiteral()) && count($field_definition->getDefaultValueLiteral())) {
$target_entity_type = \Drupal::entityManager()->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type'));
foreach ($field_definition->getDefaultValueLiteral() as $default_value) {
if (is_array($default_value) && isset($default_value['target_uuid'])) {
$entity = \Drupal::entityManager()->loadEntityByUuid($target_entity_type->id(), $default_value['target_uuid']);
// If the entity does not exist do not create the dependency.
// @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue()
if ($entity) {
$dependencies[$target_entity_type->getConfigDependencyKey()][] = $entity->getConfigDependencyName();
}
}
}
}
return $dependencies;
}
示例12: isDefaultCompatible
/**
* Determine if we should try to make a default value widget.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $definition
*
* @return bool
*/
private function isDefaultCompatible(FieldDefinitionInterface $definition) {
$class = $definition->getClass();
$type = $definition->getType();
$compatible_types = [
'boolean',
'list_float',
'list_integer',
'list_string',
];
if (in_array($type, $compatible_types)) {
return TRUE;
}
if ('entity_reference' == $type) {
// We should only store a default value in the special case
// where the target is a config entity.
// Otherwise we would have config that pointed to content.
// The site-builder could still do this in manage fields for the Update type
// but should not en-courage this practice.
$target_type = $definition->getFieldStorageDefinition()->getSetting('target_type');
$definition = $this->entityTypeManager->getDefinition($target_type);
if ($this->definitionClassImplementsInterface($definition, ['Drupal\Core\Config\Entity\ConfigEntityInterface'])) {
return TRUE;
}
}
return FALSE;
}
示例13: generateSampleValue
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$allowed_options = options_allowed_values($field_definition->getFieldStorageDefinition());
$values['value'] = array_rand($allowed_options);
return $values;
}
示例14: getMainPropertyName
/**
* {@inheritdoc}
*/
public function getMainPropertyName()
{
return $this->fieldDefinition->getFieldStorageDefinition()->getMainPropertyName();
}
示例15: purgeFieldItems
/**
* {@inheritdoc}
*/
protected function purgeFieldItems(ContentEntityInterface $entity, FieldDefinitionInterface $field_definition)
{
$storage_definition = $field_definition->getFieldStorageDefinition();
$is_deleted = $this->storageDefinitionIsDeleted($storage_definition);
$table_mapping = $this->getTableMapping();
$table_name = $table_mapping->getDedicatedDataTableName($storage_definition, $is_deleted);
$revision_name = $table_mapping->getDedicatedRevisionTableName($storage_definition, $is_deleted);
$revision_id = $this->entityType->isRevisionable() ? $entity->getRevisionId() : $entity->id();
$this->database->delete($table_name)->condition('revision_id', $revision_id)->execute();
if ($this->entityType->isRevisionable()) {
$this->database->delete($revision_name)->condition('revision_id', $revision_id)->execute();
}
}