本文整理汇总了PHP中Drupal\Component\Utility\NestedArray::setValue方法的典型用法代码示例。如果您正苦于以下问题:PHP NestedArray::setValue方法的具体用法?PHP NestedArray::setValue怎么用?PHP NestedArray::setValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Component\Utility\NestedArray
的用法示例。
在下文中一共展示了NestedArray::setValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: clearFormValues
/**
* Clear values from upload form element.
*
* @param array $element
* Upload form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state object.
*/
protected function clearFormValues(array &$element, FormStateInterface $form_state)
{
// We propagated entities to the other parts of the system. We can now remove
// them from our values.
$form_state->setValueForElement($element['upload']['fids'], '');
NestedArray::setValue($form_state->getUserInput(), $element['upload']['fids']['#parents'], '');
}
示例2: validateElement
/**
* Form validation handler for widget elements.
*
* @param array $element
* The form element.
* @param array $form_state
* The form state.
*/
public static function validateElement(array $element, FormStateInterface $form_state)
{
parent::validateElement($element, $form_state);
// Massage submitted form values.
// Drupal\Core\Field\WidgetBase::submit() expects values as
// an array of values keyed by delta first, then by column, while our
// widgets return the opposite.
if (is_array($element['#value'])) {
$values = array_values($element['#value']);
} else {
$values = array($element['#value']);
}
// Filter out the 'none' option. Use a strict comparison, because
// 0 == 'any string'.
$index = array_search('_none', $values, TRUE);
if ($index !== FALSE) {
unset($values[$index]);
}
// Transpose selections from field => delta to delta => field.
$items = array();
foreach ($values as $value) {
$items[] = array($element['#key_column'] => $value);
}
NestedArray::setValue($form_state['values'], $element['#parents'], $items);
}
示例3: testSetValueForce
/**
* Tests force-setting values.
*
* @covers ::setValue
*/
public function testSetValueForce()
{
$new_value = array('one');
$this->form['details']['non-array-parent'] = 'string';
$parents = array('details', 'non-array-parent', 'child');
NestedArray::setValue($this->form, $parents, $new_value, TRUE);
$this->assertSame($new_value, $this->form['details']['non-array-parent']['child'], 'The nested element was not forced to the new value.');
}
示例4: execute
public function execute()
{
$file = $this->getUnaliasedPath($this->configuration['in']);
$data = file_exists($file) ? YAML::decode(file_get_contents($file)) : [];
$keys = explode('/', $this->configuration['key']);
NestedArray::setValue($data, $keys, $this->configuration['value']);
file_put_contents($file, YAML::encode($data));
}
示例5: validateMultipleCount
/**
* Form element validation callback for upload element on file widget.
*
* Checks if user has uploaded more files than allowed.
*
* This validator is used only when cardinality not set to 1 or unlimited.
*
* @param array $element
* The element.
* @param FormStateInterface $form_state
* The form state.
* @param array $form
* The form.
*/
public static function validateMultipleCount($element, FormStateInterface $form_state, $form)
{
$parents = $element['#parents'];
$values = NestedArray::getValue($form_state->getValues(), $parents);
array_pop($parents);
$current = count(Element::children(NestedArray::getValue($form, $parents))) - 1;
$field_storage_definitions = \Drupal::entityManager()->getFieldStorageDefinitions($element['#entity_type']);
$field_storage = $field_storage_definitions[$element['#field_name']];
$uploaded = count($values['fids']);
$count = $uploaded + $current;
if ($count > $field_storage->getCardinality()) {
$keep = $uploaded - $count + $field_storage->getCardinality();
$removed_files = array_slice($values['fids'], $keep);
$removed_names = array();
foreach ($removed_files as $id) {
/** @var \Drupal\embridge\EmbridgeAssetEntityInterface $asset */
$asset = EmbridgeAssetEntity::load($id);
$removed_names[] = $asset->getFilename();
}
$args = array('%field' => $field_storage->getName(), '@max' => $field_storage->getCardinality(), '@count' => $uploaded, '%list' => implode(', ', $removed_names));
$message = t('Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', $args);
drupal_set_message($message, 'warning');
$values['fids'] = array_slice($values['fids'], 0, $keep);
NestedArray::setValue($form_state->getValues(), $element['#parents'], $values);
}
}
示例6: submit
/**
* Form submission handler for upload/remove button of formElement().
*
* This runs in addition to and after file_managed_file_submit().
*
* @see file_managed_file_submit()
*/
public static function submit($form, FormStateInterface $form_state)
{
// During the form rebuild, formElement() will create field item widget
// elements using re-indexed deltas, so clear out FormState::$input to
// avoid a mismatch between old and new deltas. The rebuilt elements will
// have #default_value set appropriately for the current state of the field,
// so nothing is lost in doing this.
$button = $form_state->getTriggeringElement();
$parents = array_slice($button['#parents'], 0, -2);
NestedArray::setValue($form_state->getUserInput(), $parents, NULL);
// Go one level up in the form, to the widgets container.
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
$field_name = $element['#field_name'];
$parents = $element['#field_parents'];
$submitted_values = NestedArray::getValue($form_state->getValues(), array_slice($button['#parents'], 0, -2));
foreach ($submitted_values as $delta => $submitted_value) {
if (empty($submitted_value['fids'])) {
unset($submitted_values[$delta]);
}
}
// If there are more files uploaded via the same widget, we have to separate
// them, as we display each file in it's own widget.
$new_values = array();
foreach ($submitted_values as $delta => $submitted_value) {
if (is_array($submitted_value['fids'])) {
foreach ($submitted_value['fids'] as $fid) {
$new_value = $submitted_value;
$new_value['fids'] = array($fid);
$new_values[] = $new_value;
}
} else {
$new_value = $submitted_value;
}
}
// Re-index deltas after removing empty items.
$submitted_values = array_values($new_values);
// Update form_state values.
NestedArray::setValue($form_state->getValues(), array_slice($button['#parents'], 0, -2), $submitted_values);
// Update items.
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$field_state['items'] = $submitted_values;
static::setWidgetState($parents, $field_name, $form_state, $field_state);
}
示例7: removeSubmit
/**
* Submit callback to remove an item from the field UI multiple wrapper.
*
* When a remove button is submitted, we need to find the item that it
* referenced and delete it. Since field UI has the deltas as a straight
* unbroken array key, we have to renumber everything down. Since we do this
* we *also* need to move all the deltas around in the $form_state->values
* and $form_state input so that user changed values follow. This is a bit
* of a complicated process.
*/
public static function removeSubmit($form, FormStateInterface $form_state)
{
$button = $form_state->getTriggeringElement();
$delta = $button['#delta'];
// Where in the form we'll find the parent element.
$address = array_slice($button['#array_parents'], 0, -4);
// Go one level up in the form, to the widgets container.
$parent_element = NestedArray::getValue($form, array_merge($address, array('widget')));
$field_name = $parent_element['#field_name'];
$parents = $parent_element['#field_parents'];
$field_state = static::getWidgetState($parents, $field_name, $form_state);
// Go ahead and renumber everything from our delta to the last
// item down one. This will overwrite the item being removed.
for ($i = $delta; $i <= $field_state['items_count']; $i++) {
$old_element_address = array_merge($address, array('widget', $i + 1));
$old_element_state_address = array_merge($address, array($i + 1));
$new_element_state_address = array_merge($address, array($i));
$moving_element = NestedArray::getValue($form, $old_element_address);
$moving_element_value = NestedArray::getValue($form_state->getValues(), $old_element_state_address);
$moving_element_input = NestedArray::getValue($form_state->getUserInput(), $old_element_state_address);
// Tell the element where it's being moved to.
$moving_element['#parents'] = $new_element_state_address;
// Move the element around.
$form_state->setValueForElement($moving_element, $moving_element_value);
$user_input = $form_state->getUserInput();
NestedArray::setValue($user_input, $moving_element['#parents'], $moving_element_input);
$form_state->setUserInput($user_input);
// Move the entity in our saved state.
if (isset($field_state['field_collection_item'][$i + 1])) {
$field_state['field_collection_item'][$i] = $field_state['field_collection_item'][$i + 1];
} else {
unset($field_state['field_collection_item'][$i]);
}
}
// Replace the deleted entity with an empty one. This helps to ensure that
// trying to add a new entity won't ressurect a deleted entity from the
// trash bin.
$count = count($field_state['field_collection_item']);
$field_state['field_collection_item'][$count] = entity_create('field_collection_item', array('field_name' => $field_name));
// Then remove the last item. But we must not go negative.
if ($field_state['items_count'] > 0) {
$field_state['items_count']--;
}
// Fix the weights. Field UI lets the weights be in a range of
// (-1 * item_count) to (item_count). This means that when we remove one,
// the range shrinks; weights outside of that range then get set to
// the first item in the select by the browser, floating them to the top.
// We use a brute force method because we lost weights on both ends
// and if the user has moved things around, we have to cascade because
// if I have items weight weights 3 and 4, and I change 4 to 3 but leave
// the 3, the order of the two 3s now is undefined and may not match what
// the user had selected.
$input = NestedArray::getValue($form_state->getUserInput(), $address);
// Sort by weight.
uasort($input, '_field_collection_sort_items_helper');
// Reweight everything in the correct order.
$weight = -1 * $field_state['items_count'];
foreach ($input as $key => $item) {
if ($item) {
$input[$key]['_weight'] = $weight++;
}
}
$user_input = $form_state->getUserInput();
NestedArray::setValue($user_input, $address, $input);
$form_state->setUserInput($user_input);
static::setWidgetState($parents, $field_name, $form_state, $field_state);
$form_state->setRebuild();
}
示例8: setValue
/**
* {@inheritdoc}
*/
public function setValue($element, $value, &$form_state)
{
NestedArray::setValue($form_state['values'], $element['#parents'], $value, TRUE);
}
示例9: setDestinationProperty
/**
* Sets destination properties.
*
* @param string $property
* The name of the destination property.
* @param mixed $value
* The property value to set on the destination.
*/
public function setDestinationProperty($property, $value)
{
$this->rawDestination[$property] = $value;
NestedArray::setValue($this->destination, explode(static::PROPERTY_SEPARATOR, $property), $value, TRUE);
}
示例10: setValue
/**
* Implements \Drupal\Core\Form\FormStateInterface::setValue()
*/
public function setValue($key, $value)
{
NestedArray::setValue($this->getValues(), (array) $key, $value, TRUE);
return $this;
}
示例11: updateEntityProperty
/**
* Updates a (possible nested) entity property with a value.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The config entity.
* @param array $parents
* The array of parents.
* @param string|object $value
* The value to update to.
*/
protected function updateEntityProperty(EntityInterface $entity, array $parents, $value)
{
$top_key = array_shift($parents);
$entity_value = $entity->get($top_key);
if (is_array($entity_value)) {
NestedArray::setValue($entity_value, $parents, $value);
} else {
$entity_value = $value;
}
$entity->set($top_key, $entity_value);
}
示例12: updateData
/**
* {@inheritdoc}
*/
public function updateData($key, $values = array(), $replace = FALSE)
{
if ($replace) {
if (!is_array($this->unserializedData)) {
$this->unserializedData = unserialize($this->get('data')->value);
if (!is_array($this->unserializedData)) {
$this->unserializedData = array();
}
}
NestedArray::setValue($this->unserializedData, \Drupal::service('tmgmt.data')->ensureArrayKey($key), $values);
}
foreach ($values as $index => $value) {
// In order to preserve existing values, we can not aplly the values array
// at once. We need to apply each containing value on its own.
// If $value is an array we need to advance the hierarchy level.
if (is_array($value)) {
$this->updateData(array_merge(\Drupal::service('tmgmt.data')->ensureArrayKey($key), array($index)), $value);
} else {
if (!is_array($this->unserializedData)) {
$this->unserializedData = unserialize($this->get('data')->value);
}
NestedArray::setValue($this->unserializedData, array_merge(\Drupal::service('tmgmt.data')->ensureArrayKey($key), array($index)), $value);
}
}
}
示例13: unflatten
/**
* Converts a flattened data structure into a nested array.
*
* This function can be used by translators to help with the data conversion.
*
* Nested keys will be created based on the colon, so for example
* $flattened_data['key1][key2][key3'] will be converted into
* $data['key1']['key2']['key3'].
*
* @param array $flattened_data
* The flattened data array.
*
* @return array
* The nested data array.
*/
public function unflatten(array $flattened_data)
{
$data = array();
foreach ($flattened_data as $key => $flattened_data_entry) {
NestedArray::setValue($data, explode(static::TMGMT_ARRAY_DELIMITER, $key), $flattened_data_entry);
}
return $data;
}
示例14: setValueForElement
/**
* {@inheritdoc}
*/
public function setValueForElement($element, $value)
{
$values = $this->getValues();
NestedArray::setValue($values, $element['#parents'], $value, TRUE);
$this->set('values', $values);
return $this;
}
示例15: setWidgetState
/**
* {@inheritdoc}
*/
public static function setWidgetState(array $parents, $field_name, array &$form_state, array $field_state)
{
NestedArray::setValue($form_state, static::getWidgetStateParents($parents, $field_name), $field_state);
}