本文整理汇总了PHP中Drupal\Core\Entity\EntityInterface::getComponent方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityInterface::getComponent方法的具体用法?PHP EntityInterface::getComponent怎么用?PHP EntityInterface::getComponent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Entity\EntityInterface
的用法示例。
在下文中一共展示了EntityInterface::getComponent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: copyFormValuesToEntity
/**
* {@inheritdoc}
*/
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state)
{
$form_values = $form_state->getValues();
if ($this->entity instanceof EntityWithPluginCollectionInterface) {
// Do not manually update values represented by plugin collections.
$form_values = array_diff_key($form_values, $this->entity->getPluginCollections());
}
// Collect data for 'regular' fields.
foreach ($form['#fields'] as $field_name) {
$values = $form_values['fields'][$field_name];
if ($values['type'] == 'hidden') {
$entity->removeComponent($field_name);
} else {
$options = $entity->getComponent($field_name);
// Update field settings only if the submit handler told us to.
if ($form_state->get('plugin_settings_update') === $field_name) {
// Only store settings actually used by the selected plugin.
$default_settings = $this->pluginManager->getDefaultSettings($options['type']);
$options['settings'] = isset($values['settings_edit_form']['settings']) ? array_intersect_key($values['settings_edit_form']['settings'], $default_settings) : [];
$options['third_party_settings'] = isset($values['settings_edit_form']['third_party_settings']) ? $values['settings_edit_form']['third_party_settings'] : [];
$form_state->set('plugin_settings_update', NULL);
}
$options['type'] = $values['type'];
$options['weight'] = $values['weight'];
// Only formatters have configurable label visibility.
if (isset($values['label'])) {
$options['label'] = $values['label'];
}
$entity->setComponent($field_name, $options);
}
}
// Collect data for 'extra' fields.
foreach ($form['#extra'] as $name) {
if ($form_values['fields'][$name]['type'] == 'hidden') {
$entity->removeComponent($name);
} else {
$entity->setComponent($name, array('weight' => $form_values['fields'][$name]['weight']));
}
}
}
示例2: copyFormValuesToEntity
/**
* {@inheritdoc}
*/
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state)
{
$form_values = $form_state->getValues();
if ($this->entity instanceof EntityWithPluginCollectionInterface) {
// Do not manually update values represented by plugin collections.
$form_values = array_diff_key($form_values, $this->entity->getPluginCollections());
}
// Collect data for 'regular' fields.
foreach ($form['#fields'] as $field_name) {
// Retrieve the stored field settings to merge with the incoming
// values.
$values = $form_values['fields'][$field_name];
if ($values['type'] == 'hidden') {
$entity->removeComponent($field_name);
} else {
// Get plugin settings. They lie either directly in submitted form
// values (if the whole form was submitted while some plugin settings
// were being edited), or have been persisted in $form_state.
$plugin_settings = $form_state->get('plugin_settings');
$settings = array();
if (isset($values['settings_edit_form']['settings'])) {
$settings = $values['settings_edit_form']['settings'];
} elseif (isset($plugin_settings[$field_name]['settings'])) {
$settings = $plugin_settings[$field_name]['settings'];
} elseif ($current_options = $entity->getComponent($field_name)) {
$settings = $current_options['settings'];
}
$third_party_settings = array();
if (isset($values['settings_edit_form']['third_party_settings'])) {
$third_party_settings = $values['settings_edit_form']['third_party_settings'];
} elseif (isset($plugin_settings[$field_name]['third_party_settings'])) {
$third_party_settings = $plugin_settings[$field_name]['third_party_settings'];
} elseif (($current_options = $entity->getComponent($field_name)) && isset($current_options['third_party_settings'])) {
$third_party_settings = $current_options['third_party_settings'];
}
// Only save settings actually used by the selected plugin.
$default_settings = $this->pluginManager->getDefaultSettings($values['type']);
$settings = array_intersect_key($settings, $default_settings);
// Default component values.
$component_values = array('type' => $values['type'], 'weight' => $values['weight'], 'settings' => $settings, 'third_party_settings' => $third_party_settings);
// Only formatters have configurable label visibility.
if (isset($values['label'])) {
$component_values['label'] = $values['label'];
}
$entity->setComponent($field_name, $component_values);
}
}
// Collect data for 'extra' fields.
foreach ($form['#extra'] as $name) {
if ($form_values['fields'][$name]['type'] == 'hidden') {
$entity->removeComponent($name);
} else {
$entity->setComponent($name, array('weight' => $form_values['fields'][$name]['weight']));
}
}
}