/** * Submit callback for remove buttons. * * @param array $form * @param \Drupal\Core\Form\FormStateInterface $form_state */ public static function removeItemSubmit(array &$form, FormStateInterface $form_state) { $triggering_element = $form_state->getTriggeringElement(); // Remove weight of entity being removed. $form_state->unsetValue(['selected', $triggering_element['#attributes']['data-remove-entity']]); // Remove entity itself. $selected_entities =& $form_state->get(['entity_browser', 'selected_entities']); unset($selected_entities[$triggering_element['#attributes']['data-row-id']]); static::saveNewOrder($form_state); $form_state->setRebuild(); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_connection_settings = $form_state->getValue('connection_settings'); switch ($form_state->getTriggeringElement()['#name']) { case 'process_updates': // Save the connection settings to the DB. $filetransfer_backend = $form_connection_settings['authorize_filetransfer_default']; // If the database is available then try to save our settings. We have // to make sure it is available since this code could potentially (will // likely) be called during the installation process, before the // database is set up. try { $connection_settings = array(); foreach ($form_connection_settings[$filetransfer_backend] as $key => $value) { // We do *not* want to store passwords in the database, unless the // backend explicitly says so via the magic #filetransfer_save form // property. Otherwise, we store everything that's not explicitly // marked with #filetransfer_save set to FALSE. if (!isset($form['connection_settings'][$filetransfer_backend][$key]['#filetransfer_save'])) { if ($form['connection_settings'][$filetransfer_backend][$key]['#type'] != 'password') { $connection_settings[$key] = $value; } } elseif ($form['connection_settings'][$filetransfer_backend][$key]['#filetransfer_save']) { $connection_settings[$key] = $value; } } // Set this one as the default authorize method. $this->config('system.authorize')->set('filetransfer_default', $filetransfer_backend); // Save the connection settings minus the password. $this->config('system.authorize')->set('filetransfer_connection_settings_' . $filetransfer_backend, $connection_settings); $filetransfer = $this->getFiletransfer($filetransfer_backend, $form_connection_settings[$filetransfer_backend]); // Now run the operation. $this->runOperation($filetransfer); } catch (\Exception $e) { // If there is no database available, we don't care and just skip // this part entirely. } break; case 'enter_connection_settings': $form_state->setRebuild(); break; case 'change_connection_type': $form_state->setRebuild(); $form_state->unsetValue(array('connection_settings', 'authorize_filetransfer_default')); break; } }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { if (!$form_state->hasValue('context')) { $form_state->setValue('context', xmlsitemap_get_current_context()); } if ($form_state->hasValue(['context', 'language'])) { $language = $form_state->getValue(['context', 'language']); if ($language == LanguageInterface::LANGCODE_NOT_SPECIFIED) { $form_state->unsetValue(['context', 'language']); } } $context = $form_state->getValue('context'); $this->entity->context = $context; $this->entity->label = $form_state->getValue('label'); $this->entity->id = xmlsitemap_sitemap_get_context_hash($context); try { $status = $this->entity->save(); if ($status == SAVED_NEW) { drupal_set_message($this->t('Saved the %label sitemap.', array('%label' => $this->entity->label()))); } else { if ($status == SAVED_UPDATED) { drupal_set_message($this->t('Updated the %label sitemap.', array('%label' => $this->entity->label()))); } } } catch (EntityStorageException $ex) { drupal_set_message($this->t('There is another sitemap saved with the same context.'), 'error'); } $form_state->setRedirect('xmlsitemap.admin_search'); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; $widgets = []; foreach ($this->widgetManager->getDefinitions() as $plugin_id => $plugin_definition) { $widgets[$plugin_id] = $plugin_definition['label']; } $default_widgets = []; foreach ($entity_browser->getWidgets() as $widget) { /** @var \Drupal\entity_browser\WidgetInterface $widget */ $default_widgets[] = $widget->id(); } $form['widget'] = ['#type' => 'select', '#title' => $this->t('Add widget plugin'), '#options' => ['_none_' => '- ' . $this->t('Select a widget to add it') . ' -'] + $widgets, '#ajax' => ['callback' => [get_class($this), 'tableUpdatedAjaxCallback'], 'wrapper' => 'widgets', 'event' => 'change'], '#executes_submit_callback' => TRUE, '#submit' => [[get_class($this), 'submitAddWidget']], '#limit_validation_errors' => [['widget']]]; $form_state->unsetValue('widget'); $form['widgets'] = ['#type' => 'container', '#attributes' => ['id' => 'widgets']]; $form['widgets']['table'] = ['#type' => 'table', '#header' => [$this->t('Form'), $this->t('Operations'), $this->t('Actions'), $this->t('Weight')], '#empty' => $this->t('There are no widgets.'), '#tabledrag' => [['action' => 'order', 'relationship' => 'sibling', 'group' => 'variant-weight']]]; /** @var \Drupal\entity_browser\WidgetInterface $widget */ foreach ($entity_browser->getWidgets() as $uuid => $widget) { $row = ['#attributes' => ['class' => ['draggable']]]; $row['label'] = ['#type' => 'textfield', '#default_value' => $widget->label(), '#title' => $this->t('Label')]; $row['form'] = []; $row['form'] = $widget->buildConfigurationForm($row['form'], $form_state); $row['remove'] = ['#type' => 'submit', '#value' => $this->t('Delete'), '#name' => 'remove' . $uuid, '#ajax' => ['callback' => [get_class($this), 'tableUpdatedAjaxCallback'], 'wrapper' => 'widgets', 'event' => 'click'], '#executes_submit_callback' => TRUE, '#submit' => [[get_class($this), 'submitDeleteWidget']], '#arguments' => $uuid]; $row['weight'] = ['#type' => 'weight', '#default_value' => $widget->getWeight(), '#title' => $this->t('Weight for @widget widget', ['@widget' => $widget->label()]), '#title_display' => 'invisible', '#attributes' => ['class' => ['variant-weight']]]; $form['widgets']['table'][$uuid] = $row; } return $form; }
/** * {@inheritdoc} */ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { // Variants will be handled independently. $variants = $form_state->getValue('variants'); $form_state->unsetValue('variants'); parent::copyFormValuesToEntity($entity, $form, $form_state); $form_state->setValue('variants', $variants); }
/** * {@inheritdoc} */ public function view(OrderInterface $order, array $form, FormStateInterface $form_state) { $user = \Drupal::currentUser(); $pane = $this->pluginDefinition['id']; $source = $this->sourcePaneId(); $contents['#description'] = $this->getDescription(); if ($source != $pane) { $contents['copy_address'] = array('#type' => 'checkbox', '#title' => $this->getCopyAddressText(), '#default_value' => $this->configuration['default_same_address'], '#ajax' => array('callback' => array($this, 'ajaxRender'), 'wrapper' => $pane . '-address-pane', 'progress' => array('type' => 'throbber'))); } if ($user->isAuthenticated() && ($addresses = uc_select_addresses($user->id(), $pane))) { $contents['select_address'] = array('#type' => 'select', '#title' => t('Saved addresses'), '#options' => $addresses['#options'], '#ajax' => array('callback' => array($this, 'ajaxRender'), 'wrapper' => $pane . '-address-pane', 'progress' => array('type' => 'throbber')), '#states' => array('invisible' => array('input[name="panes[' . $pane . '][copy_address]"]' => array('checked' => TRUE)))); } $contents['address'] = array('#type' => 'uc_address', '#default_value' => $order->getAddress($pane), '#prefix' => '<div id="' . $pane . '-address-pane">', '#suffix' => '</div>'); if ($form_state->hasValue(['panes', $pane, 'copy_address'])) { $contents['address']['#hidden'] = !$form_state->isValueEmpty(['panes', $pane, 'copy_address']); } elseif (isset($contents['copy_address'])) { $contents['address']['#hidden'] = $this->configuration['default_same_address']; } if ($element = $form_state->getTriggeringElement()) { $input = $form_state->getUserInput(); if ($element['#name'] == "panes[{$pane}][copy_address]") { $address =& $form_state->getValue(['panes', $source]); foreach ($address as $field => $value) { if (substr($field, 0, strlen($source)) == $source) { $field = str_replace($source, $pane, $field); $input['panes'][$pane][$field] = $value; $order->{$field} = $value; } } } if ($element['#name'] == "panes[{$pane}][select_address]") { $address = $addresses[$element['#value']]; foreach ($address as $field => $value) { $input['panes'][$pane][$pane . '_' . $field] = $value; $order->{$pane . '_' . $field} = $value; } } $form_state->setUserInput($input); // Forget any previous Ajax submissions, as we send new default values. $form_state->unsetValue('uc_address'); } return $contents; }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { $values = $form_state->getValues(); if ($values['smtp_on'] == 1 && $values['smtp_host'] == '') { $form_state->setErrorByName('smtp_host', $this->t('You must enter an SMTP server address.')); } if ($values['smtp_on'] == 1 && $values['smtp_port'] == '') { $form_state->setErrorByName('smtp_port', $this->t('You must enter an SMTP port number.')); } if ($values['smtp_from'] && !valid_email_address($values['smtp_from'])) { $form_state->setErrorByName('smtp_from', $this->t('The provided from e-mail address is not valid.')); } if ($values['smtp_test_address'] && !valid_email_address($values['smtp_test_address'])) { $form_state->setErrorByName('smtp_test_address', $this->t('The provided test e-mail address is not valid.')); } // If username is set empty, we must set both username/password empty as well. if (empty($values['smtp_username'])) { $values['smtp_password'] = ''; } elseif (empty($values['smtp_password'])) { $form_state->unsetValue('smtp_password'); } }
/** * @covers ::unsetValue */ public function testUnsetValue() { $key = 'FOO'; $this->decoratedFormState->unsetValue($key)->shouldBeCalled(); $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->unsetValue($key)); }
/** * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { if (!$form_state->isValueEmpty('views_label_checkbox')) { $this->configuration['views_label'] = $form_state->getValue('views_label'); } else { $this->configuration['views_label'] = ''; } $form_state->unsetValue('views_label_checkbox'); }
/** * {@inheritdoc} * * Most block plugins should not override this method. To add validation * for a specific block type, override BlockBase::blockValidate(). * * @see \Drupal\Core\Block\BlockBase::blockValidate() */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { // Remove the admin_label form item element value so it will not persist. $form_state->unsetValue('admin_label'); // Transform the #type = checkboxes value to a numerically indexed array. $contexts = $form_state->getValue(array('cache', 'contexts')); $form_state->setValue(array('cache', 'contexts'), array_values(array_filter($contexts))); $this->blockValidate($form, $form_state); }
/** * {@inheritdoc} */ public function unsetValue($key) { $this->decoratedFormState->unsetValue($key); return $this; }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { $values = $form_state->getValues(); if ($values['smtp_on'] == 1 && $values['smtp_host'] == '') { $form_state->setErrorByName('smtp_host', $this->t('You must enter an SMTP server address.')); } if ($values['smtp_on'] == 1 && $values['smtp_port'] == '') { $form_state->setErrorByName('smtp_port', $this->t('You must enter an SMTP port number.')); } if ($values['smtp_from'] && !valid_email_address($values['smtp_from'])) { $form_state->setErrorByName('smtp_from', $this->t('The provided from e-mail address is not valid.')); } if ($values['smtp_test_address'] && !valid_email_address($values['smtp_test_address'])) { $form_state->setErrorByName('smtp_test_address', $this->t('The provided test e-mail address is not valid.')); } // If username is set empty, we must set both username/password empty as well. if (empty($values['smtp_username'])) { $values['smtp_password'] = ''; } // A little hack. When form is presented, the password is not shown (Drupal way of doing). // So, if user submits the form without changing the password, we must prevent it from being reset. elseif (empty($values['smtp_password'])) { $form_state->unsetValue('smtp_password'); } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $entity = $form['xmlsitemap']['#entity']; $bundle = $form['xmlsitemap']['#bundle']; // Handle new bundles by fetching the proper bundle key value from the form // state values. if (empty($bundle)) { $entity_info = $form['xmlsitemap']['#entity_info']; if (isset($entity_info['bundle keys']['bundle'])) { $bundle_key = $entity_info['bundle keys']['bundle']; if ($form_state->hasValue($bundle_key)) { $bundle = $form_state->getValue($bundle_key); $form['xmlsitemap']['#bundle'] = $bundle; } } } $xmlsitemap = $form_state->getValue('xmlsitemap'); xmlsitemap_link_bundle_settings_save($this->entity_type, $this->bundle_type, $xmlsitemap, TRUE); $entity_info = $form['xmlsitemap']['#entity_info']; if (!empty($form['xmlsitemap']['#show_message'])) { drupal_set_message($this->t('XML sitemap settings for the %bundle have been saved.', array('%bundle' => $entity_info['bundles'][$bundle]['label']))); } // Unset the form values since we have already saved the bundle settings and // we don't want these values to get saved as configuration, depending on how // the form saves the form values. $form_state->unsetValue('xmlsitemap'); parent::submitForm($form, $form_state); }
/** * {@inheritdoc} */ public function validate(array $form, FormStateInterface $form_state) { // Only validate on edit. if ($form_state->hasValue('keyed_mappings')) { // Check if another breakpoint group is selected. if ($form_state->getValue('breakpointGroup') != $form_state->getCompleteForm()['breakpointGroup']['#default_value']) { // Remove the mappings since the breakpoint ID has changed. $form_state->unsetValue('keyed_mappings'); } } }
/** * Handles form submission for the views block configuration form. * * @param \Drupal\views\Plugin\Block\ViewsBlock $block * The ViewsBlock plugin. * @param array $form * The form definition array for the full block configuration form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * * @see \Drupal\views\Plugin\Block\ViewsBlock::blockSubmit() */ public function blockSubmit(ViewsBlock $block, $form, FormStateInterface $form_state) { if ($items_per_page = $form_state->getValue(array('override', 'items_per_page'))) { $block->setConfigurationValue('items_per_page', $items_per_page); } $form_state->unsetValue(array('override', 'items_per_page')); }
/** * #element_validate handler for the "file_upload" element in settingsForm(). * * Moves the text editor's file upload settings from the DrupalFile plugin's * own settings into $editor->file_upload. * * @see \Drupal\editor\Form\EditorFileDialog * @see editor_file_upload_settings_form() */ function validateFileUploadSettings(array $element, FormStateInterface $form_state) { $settings =& $form_state->getValue(array('editor', 'settings', 'plugins', 'drupalfile', 'file_upload')); $editor = $form_state->get('editor'); foreach ($settings as $key => $value) { if (!empty($value)) { $editor->setThirdPartySetting('editor_file', $key, $value); } else { $editor->unsetThirdPartySetting('editor_file', $key); } } $form_state->unsetValue(array('editor', 'settings', 'plugins', 'drupalfile')); }
/** * Form element validation handler; Stores the new values in the form state. * * @param array $form * The form where the settings form is being included in. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state of the (entire) configuration form. */ public static function fieldSettingsFormValidate(array $form, FormStateInterface $form_state) { if ($form_state->hasValue('field')) { $form_state->unsetValue(array('field', 'settings', 'handler_submit')); $form_state->get('field')->settings = $form_state->getValue(['field', 'settings']); $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($form_state->get('field')); $handler->validateConfigurationForm($form, $form_state); } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_connection_settings = $form_state->getValue('connection_settings'); switch ($form_state->getTriggeringElement()['#name']) { case 'process_updates': // Save the connection settings to the DB. $filetransfer_backend = $form_connection_settings['authorize_filetransfer_default']; // If the database is available then try to save our settings. We have // to make sure it is available since this code could potentially (will // likely) be called during the installation process, before the // database is set up. try { $filetransfer = $this->getFiletransfer($filetransfer_backend, $form_connection_settings[$filetransfer_backend]); // Now run the operation. $response = $this->runOperation($filetransfer); if ($response instanceof Response) { $form_state->setResponse($response); } } catch (\Exception $e) { // If there is no database available, we don't care and just skip // this part entirely. } break; case 'enter_connection_settings': $form_state->setRebuild(); break; case 'change_connection_type': $form_state->setRebuild(); $form_state->unsetValue(array('connection_settings', 'authorize_filetransfer_default')); break; } }
/** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { $values = $form_state->getValues(); // Since the form is nested into another, we can't simply use #parents for // doing this array restructuring magic. (At least not without creating an // unnecessary dependency on internal implementation.) $values += $values['http']; $values += $values['advanced']; $values += !empty($values['autocomplete']) ? $values['autocomplete'] : array(); unset($values['http'], $values['advanced'], $values['autocomplete']); // Highlighting retrieved data only makes sense when we retrieve data. $values['highlight_data'] &= $values['retrieve_data']; // For password fields, there is no default value, they're empty by default. // Therefore we ignore empty submissions if the user didn't change either. if ($values['http_pass'] === '' && isset($this->configuration['http_user']) && $values['http_user'] === $this->configuration['http_user']) { $values['http_pass'] = $this->configuration['http_pass']; } foreach ($values as $key => $value) { $form_state->setValue($key, $value); } // The server description is a #type item element, which means it has a // value, do not save it. $form_state->unsetValue('server_description'); parent::submitConfigurationForm($form, $form_state); }
/** * {@inheritdoc} * * Most block plugins should not override this method. To add validation * for a specific block type, override BlockBase::blockValidate(). * * @see \Drupal\Core\Block\BlockBase::blockValidate() */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { // Remove the admin_label form item element value so it will not persist. $form_state->unsetValue('admin_label'); // Transform the #type = checkboxes value to a numerically indexed array. $contexts = $form_state->getValue(array('cache', 'contexts')); $form_state->setValue(array('cache', 'contexts'), array_values(array_filter($contexts))); foreach ($this->getVisibilityConditions() as $condition_id => $condition) { // Allow the condition to validate the form. $condition_values = (new FormState())->setValues($form_state->getValue(['visibility', $condition_id])); $condition->validateConfigurationForm($form, $condition_values); // Update the original form values. $form_state->setValue(['visibility', $condition_id], $condition_values->getValues()); } $this->blockValidate($form, $form_state); }
/** * #element_validate handler for the "image_upload" element in settingsForm(). * * Moves the text editor's image upload settings from the DrupalImage plugin's * own settings into $editor->image_upload. * * @see \Drupal\editor\Form\EditorImageDialog * @see editor_image_upload_settings_form() */ function validateImageUploadSettings(array $element, FormStateInterface $form_state) { $settings =& $form_state->getValue(array('editor', 'settings', 'plugins', 'drupalimage', 'image_upload')); $form_state->get('editor')->setImageUploadSettings($settings); $form_state->unsetValue(array('editor', 'settings', 'plugins', 'drupalimage')); }
/** * {@inheritdoc} */ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { $keys_to_ignore = ['variants', 'parameters']; $values_to_restore = []; foreach ($keys_to_ignore as $key) { $values_to_restore[$key] = $form_state->getValue($key); $form_state->unsetValue($key); } parent::copyFormValuesToEntity($entity, $form, $form_state); foreach ($values_to_restore as $key => $value) { $form_state->setValue($key, $value); } }
/** * {@inheritdoc} * * Most block plugins should not override this method. To add validation * for a specific block type, override BlockBase::blockValidate(). * * @see \Drupal\Core\Block\BlockBase::blockValidate() */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { // Remove the admin_label form item element value so it will not persist. $form_state->unsetValue('admin_label'); $this->blockValidate($form, $form_state); }
/** * {@inheritdoc} */ public function validate(array $form, FormStateInterface $form_state) { // Only validate on edit. if ($form_state->hasValue('keyed_styles')) { // Check if another breakpoint group is selected. if ($form_state->getValue('breakpoint_group') != $form_state->getCompleteForm()['breakpoint_group']['#default_value']) { // Remove the image style mappings since the breakpoint ID has changed. $form_state->unsetValue('keyed_styles'); } // @todo Filter 'sizes_image_styles' to a normal array in // https://www.drupal.org/node/2334387. For an example see // \Drupal\Core\Block\BlockBase::validateConfigurationForm(). } }
/** * Simple submit handler */ public function submitOptionsForm(&$form, FormStateInterface $form_state) { // Do not store these values. $form_state->unsetValue('expose_button'); $form_state->unsetValue('group_button'); if (!$this->isAGroup()) { $this->operatorSubmit($form, $form_state); $this->valueSubmit($form, $form_state); } if (!empty($this->options['exposed'])) { $this->submitExposeForm($form, $form_state); } if ($this->isAGroup()) { $this->buildGroupSubmit($form, $form_state); } }
/** * {@inheritdoc} */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { // If no checkboxes were checked for 'target_bundles', store NULL ("all // bundles are referenceable") rather than empty array ("no bundle is // referenceable" - typically happens when all referenceable bundles have // been deleted). if ($form_state->getValue(['settings', 'handler_settings', 'target_bundles']) === []) { $form_state->setValue(['settings', 'handler_settings', 'target_bundles'], NULL); } // Don't store the 'target_bundles_update' button value into the field // config settings. $form_state->unsetValue(['settings', 'handler_settings', 'target_bundles_update']); }
/** * Simple submit handler */ public function submitOptionsForm(&$form, FormStateInterface $form_state) { // Do not store this values. $form_state->unsetValue('expose_button'); $this->sortSubmit($form, $form_state); if (!empty($this->options['exposed'])) { $this->submitExposeForm($form, $form_state); } }
/** * {@inheritdoc} */ public function settingsFormSubmit(array $form, FormStateInterface $form_state) { // Modify the toolbar settings by reference. The values in // $form_state->getValue(array('editor', 'settings')) will be saved directly // by editor_form_filter_admin_format_submit(). $toolbar_settings =& $form_state->getValue(array('editor', 'settings', 'toolbar')); // The rows key is not built into the form structure, so decode the button // groups data into this new key and remove the button_groups key. $toolbar_settings['rows'] = json_decode($toolbar_settings['button_groups'], TRUE); unset($toolbar_settings['button_groups']); // Remove the plugin settings' vertical tabs state; no need to save that. if ($form_state->hasValue(array('editor', 'settings', 'plugins'))) { $form_state->unsetValue(array('editor', 'settings', 'plugin_settings')); } }
public function validateOptionsForm(&$form, FormStateInterface $form_state) { // Don't run validation on style plugins without the grouping setting. if ($form_state->hasValue(array('style_options', 'grouping'))) { // Don't save grouping if no field is specified. $groupings = $form_state->getValue(array('style_options', 'grouping')); foreach ($groupings as $index => $grouping) { if (empty($grouping['field'])) { $form_state->unsetValue(array('style_options', 'grouping', $index)); } } } }
/** * {@inheritdoc} */ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { // Subscriptions are handled later, in the submit callbacks through // ::getSelectedNewsletters(). Letting them be copied here would break // subscription management. $subsciptions_value = $form_state->getValue('subscriptions'); $form_state->unsetValue('subscriptions'); parent::copyFormValuesToEntity($entity, $form, $form_state); $form_state->setValue('subscriptions', $subsciptions_value); }