/** * Form element validation handler for 'name' in form_test_validate_form(). */ public function validateName(&$element, FormStateInterface $form_state) { $triggered = FALSE; if ($form_state->getValue('name') == 'element_validate') { // Alter the form element. $element['#value'] = '#value changed by #element_validate'; // Alter the submitted value in $form_state. $form_state->setValueForElement($element, 'value changed by setValueForElement() in #element_validate'); $triggered = TRUE; } if ($form_state->getValue('name') == 'element_validate_access') { $form_state->set('form_test_name', $form_state->getValue('name')); // Alter the form element. $element['#access'] = FALSE; $triggered = TRUE; } elseif ($form_state->has('form_test_name')) { // To simplify this test, just take over the element's value into $form_state. $form_state->setValueForElement($element, $form_state->get('form_test_name')); $triggered = TRUE; } if ($triggered) { // Output the element's value from $form_state. drupal_set_message(t('@label value: @value', array('@label' => $element['#title'], '@value' => $form_state->getValue('name')))); // Trigger a form validation error to see our changes. $form_state->setErrorByName(''); } }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $view = $this->entity; $form['#prefix'] = '<div id="views-preview-wrapper" class="views-admin clearfix">'; $form['#suffix'] = '</div>'; $form['#id'] = 'views-ui-preview-form'; $form_state->disableCache(); $form['controls']['#attributes'] = array('class' => array('clearfix')); $form['controls']['title'] = array('#prefix' => '<h2 class="view-preview-form__title">', '#markup' => $this->t('Preview'), '#suffix' => '</h2>'); // Add a checkbox controlling whether or not this display auto-previews. $form['controls']['live_preview'] = array('#type' => 'checkbox', '#id' => 'edit-displays-live-preview', '#title' => $this->t('Auto preview'), '#default_value' => \Drupal::config('views.settings')->get('ui.always_live_preview')); // Add the arguments textfield $form['controls']['view_args'] = array('#type' => 'textfield', '#title' => $this->t('Preview with contextual filters:'), '#description' => $this->t('Separate contextual filter values with a "/". For example, %example.', array('%example' => '40/12/10')), '#id' => 'preview-args'); $args = array(); if (!$form_state->isValueEmpty('view_args')) { $args = explode('/', $form_state->getValue('view_args')); } $user_input = $form_state->getUserInput(); if ($form_state->get('show_preview') || !empty($user_input['js'])) { $form['preview'] = array('#weight' => 110, '#theme_wrappers' => array('container'), '#attributes' => array('id' => 'views-live-preview'), 'preview' => $view->renderPreview($this->displayID, $args)); } $uri = $view->urlInfo('preview-form'); $uri->setRouteParameter('display_id', $this->displayID); $form['#action'] = $uri->toString(); return $form; }
public function buildFieldsForm(array &$form, FormStateInterface $form_state) { if (!$form_state->has('fields')) { $form_state->set('fields', $this->configuration['fields']); } $form_state_fields = $form_state->get('fields'); // Check if we need to add a new field, or remove one. $triggering_element = $form_state->getTriggeringElement(); if (isset($triggering_element['#name'])) { drupal_set_message(t('Changes in this form will not be saved until the %button button at the form bottom is clicked.', array('%button' => t('Save'))), 'warning'); $button_name = $triggering_element['#name']; if ($button_name == 'add_aggregation_field') { // Increment $i until the corresponding field is not set, then create // the field with that number as suffix. for ($i = 1; isset($form_state_fields['search_api_aggregation_' . $i]); ++$i) { } $form_state_fields['search_api_aggregation_' . $i] = array('label' => '', 'type' => 'union', 'fields' => array()); } else { // Get the field ID from the button name. $field_id = substr($button_name, 25); unset($form_state_fields[$field_id]); } $form_state->set('fields', $form_state_fields); } // Get index type descriptions. $type_descriptions = $this->getTypeDescriptions(); $types = $this->getTypes(); // Get the available fields for this index. $fields = $this->index->getFields(FALSE); $field_options = array(); $field_properties = array(); // Annotate them so we can show them cleanly in the UI. // @todo Use option groups to group fields by datasource? /** @var \Drupal\search_api\Item\FieldInterface[] $fields */ foreach ($fields as $field_id => $field) { $field_options[$field_id] = $field->getPrefixedLabel(); $field_properties[$field_id] = array('#attributes' => array('title' => $field_id), '#description' => $field->getDescription()); } ksort($field_options); $form['fields'] = array('#type' => 'container', '#attributes' => array('id' => 'search-api-alter-add-aggregation-field-settings'), '#tree' => TRUE); foreach ($form_state_fields as $field_id => $field) { $new = !$field['label']; $form['fields'][$field_id] = array('#type' => 'details', '#title' => $new ? $this->t('New field') : $field['label'], '#open' => $new); $form['fields'][$field_id]['label'] = array('#type' => 'textfield', '#title' => $this->t('New field name'), '#default_value' => $field['label'], '#required' => TRUE); $form['fields'][$field_id]['type'] = array('#type' => 'select', '#title' => $this->t('Aggregation type'), '#options' => $types, '#default_value' => $field['type'], '#required' => TRUE); $form['fields'][$field_id]['type_descriptions'] = $type_descriptions; foreach (array_keys($types) as $type) { // @todo This shouldn't rely on undocumented form array structure. $form['fields'][$field_id]['type_descriptions'][$type]['#states']['visible'][':input[name="processors[aggregated_field][settings][fields][' . $field_id . '][type]"]']['value'] = $type; } // @todo Order checked fields first in list? $form['fields'][$field_id]['fields'] = array_merge($field_properties, array('#type' => 'checkboxes', '#title' => $this->t('Contained fields'), '#options' => $field_options, '#default_value' => array_combine($field['fields'], $field['fields']), '#attributes' => array('class' => array('search-api-checkboxes-list')), '#required' => TRUE)); $form['fields'][$field_id]['actions'] = array('#type' => 'actions', 'remove' => array('#type' => 'submit', '#value' => $this->t('Remove field'), '#submit' => array(array($this, 'submitAjaxFieldButton')), '#limit_validation_errors' => array(), '#name' => 'remove_aggregation_field_' . $field_id, '#ajax' => array('callback' => array($this, 'buildAjaxAddFieldButton'), 'wrapper' => 'search-api-alter-add-aggregation-field-settings'))); } }
/** * {@inheritdoc} */ protected function valueForm(&$form, FormStateInterface $form_state) { // @todo Hopefully we can now be more sure of what we get in $this->value. while (is_array($this->value) && count($this->value) < 2) { $this->value = $this->value ? reset($this->value) : NULL; } $form['value'] = array( '#type' => 'textfield', '#title' => !$form_state->get('exposed') ? $this->t('Value') : '', '#size' => 30, '#default_value' => isset($this->value) ? $this->value : '', ); // Hide the value box if the operator is 'empty' or 'not empty'. // Radios share the same selector so we have to add some dummy selector. if (!$form_state->get('exposed')) { $form['value']['#states']['visible'] = array( ':input[name="options[operator]"],dummy-empty' => array('!value' => 'empty'), ':input[name="options[operator]"],dummy-not-empty' => array('!value' => 'not empty'), ); } elseif (!empty($this->options['expose']['use_operator'])) { $name = $this->options['expose']['operator_id']; $form['value']['#states']['visible'] = array( ':input[name="' . $name . '"],dummy-empty' => array('!value' => 'empty'), ':input[name="' . $name . '"],dummy-not-empty' => array('!value' => 'not empty'), ); } }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $display_id = NULL) { if (isset($display_id) && $form_state->has('display_id') && $display_id !== $form_state->get('display_id')) { throw new \InvalidArgumentException('Mismatch between $form_state->get(\'display_id\') and $display_id.'); } $this->displayID = $form_state->has('display_id') ? $form_state->get('display_id') : $display_id; return parent::buildForm($form, $form_state); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $params['from'] = \Drupal::service('simplenews.mailer')->getFrom(); $params['context'] = $form_state->get('context'); $subscriber = $params['context']['simplenews_subscriber']; \Drupal::service('plugin.manager.mail')->mail('simplenews', $form_state->get('key'), $subscriber->getMail(), $subscriber->getLangcode(), $params, $params['from']['address']); drupal_set_message(t('The confirmation mail has been sent.')); $form_state->setRedirect('<front>'); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_state->setCached(); $form_state->setRebuild(); $database_class = $form_state->get('database_class'); if ($form_state->get('database') instanceof $database_class) { $form_state->set('database_connection_found', TRUE); } }
/** * {@inheritdoc] */ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\Core\Entity\EntityInterface $entity_1 */ $entity_1 = $form_state->get('entity_1'); $entity_1->save(); /** @var \Drupal\Core\Entity\EntityInterface $entity_2 */ $entity_2 = $form_state->get('entity_2'); $entity_2->save(); drupal_set_message($this->t('test_entities @id_1 and @id_2 have been updated.', array('@id_1' => $entity_1->id(), '@id_2' => $entity_2->id()))); }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { // The page might have been serialized, resulting in a new display variant // collection. Refresh the display variant and block objects. $this->displayVariant = $this->page->getVariant($form_state->get('display_variant_id')); $this->block = $this->displayVariant->getBlock($form_state->get('block_id')); $settings = (new FormState())->setValues($form_state->getValue('settings')); // Call the plugin validate handler. $this->block->validateConfigurationForm($form, $settings); // Update the original form values. $form_state->setValue('settings', $settings->getValues()); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $view = $form_state->get('view'); $item =& $form_state->get('handler')->options; $type = $form_state->get('type'); $handler = Views::handlerManager($type)->getHandler($item); $executable = $view->getExecutable(); $handler->init($executable, $executable->display_handler, $item); $handler->submitGroupByForm($form, $form_state); // Store the item back on the view $executable->setHandler($form_state->get('display_id'), $form_state->get('type'), $form_state->get('id'), $item); // Write to cache $view->cacheSet(); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $list = $form_state->get('list'); $webhook_actions = $form_state->getValue('webhook_actions'); $actions = array(); foreach ($webhook_actions as $webhook_id => $enable) { $actions[$webhook_id] = $enable === 1; } $result = FALSE; if (count($actions) > 0) { $webhook_url = mailchimp_webhook_url(); $webhooks = mailchimp_webhook_get($list['id']); if (!empty($webhooks)) { foreach ($webhooks as $webhook) { if ($webhook['url'] == $webhook_url) { // Delete current webhook. mailchimp_webhook_delete($list['id'], mailchimp_webhook_url()); } } } // Add webhook with enabled actions. $result = mailchimp_webhook_add($list['id'], mailchimp_webhook_url(), $actions); } if ($result) { drupal_set_message(t('Webhooks for list "%name" have been updated.', array('%name' => $list['name']))); } else { drupal_set_message(t('Unable to update webhooks for list "%name".', array('%name' => $list['name'])), 'warning'); } $form_state->setRedirect('mailchimp_lists.overview'); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Invoke hook_uc_order($op = 'submit') to test to make sure the order can // be completed... used for auto payment in uc_credit.module. $order = $form_state->get('uc_order'); $error = FALSE; // Invoke it on a per-module basis instead of all at once. $module_handler = \Drupal::moduleHandler(); foreach ($module_handler->getImplementations('uc_order') as $module) { $function = $module . '_uc_order'; if (function_exists($function)) { // $order must be passed by reference. $result = $function('submit', $order, NULL); $msg_type = 'status'; if ($result[0]['pass'] === FALSE) { $error = TRUE; $msg_type = 'error'; } if (!empty($result[0]['message'])) { drupal_set_message($result[0]['message'], $msg_type); } // Stop invoking the hooks if there was an error. if ($error) { break; } } } if ($error) { $form_state->setRedirect('uc_cart.checkout_review'); } else { unset($_SESSION['uc_checkout'][$order->id()]['do_review']); $_SESSION['uc_checkout'][$order->id()]['do_complete'] = TRUE; $form_state->setRedirect('uc_cart.checkout_complete'); } }
/** * {@inheritdoc} */ public function &get($property) { if (isset(self::$inheritedKeys[$property])) { return $this->mainFormState->get($property); } $value = &NestedArray::getValue($this->internalStorage, (array) $property); return $value; }
/** * {@inheritdoc} */ public function getSourceLangcode(FormStateInterface $form_state) { if ($source = $form_state->get(['content_translation', 'source'])) { return $source->getId(); } return FALSE; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $key = 'payment_reference_element_prototype_payment'; if ($form_state->has($key)) { $prototype_payment = $form_state->get($key); /** @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference $payment_type */ $payment_type = $prototype_payment->getPaymentType(); } else { $entity_type_id = 'user'; $bundle = 'user'; $field_name = 'foobarbaz'; /** @var \Drupal\payment\Entity\PaymentInterface $prototype_payment */ $prototype_payment = entity_create('payment', array('bundle' => 'payment_reference')); $prototype_payment->setCurrencyCode('EUR')->setOwnerId(2)->setLineItems(Generate::createPaymentLineItems()); /** @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference $payment_type */ $payment_type = $prototype_payment->getPaymentType(); $payment_type->setEntityTypeId($entity_type_id); $payment_type->setBundle($bundle); $payment_type->setFieldName($field_name); $form_state->set($key, $prototype_payment); } $form['payment_reference'] = array('#plugin_selector_id' => 'payment_select_list', '#prototype_payment' => $prototype_payment, '#queue_category_id' => $payment_type->getEntityTypeId() . '.' . $payment_type->getBundle() . '.' . $payment_type->getFieldName(), '#queue_owner_id' => 2, '#required' => TRUE, '#title' => 'FooBarBaz', '#type' => 'payment_reference'); $form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); return $form; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface $line_item */ $line_item = $form_state->get('payment_line_item'); $line_item->submitConfigurationForm($form['line_item'], $form_state); $form_state->setRedirect('user.login'); }
/** * Add a type selector to the value form */ protected function valueForm(&$form, FormStateInterface $form_state) { if (!$form_state->get('exposed')) { $form['value']['type'] = array('#type' => 'radios', '#title' => $this->t('Value type'), '#options' => array('date' => $this->t('A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.'), 'offset' => $this->t('An offset from the current time such as "@example1" or "@example2"', array('@example1' => '+1 day', '@example2' => '-2 hours -30 minutes'))), '#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date'); } parent::valueForm($form, $form_state); }
/** * Process handler for the language_configuration form element. */ public static function processLanguageConfiguration(&$element, FormStateInterface $form_state, &$form) { $options = isset($element['#options']) ? $element['#options'] : array(); // Avoid validation failure since we are moving the '#options' key in the // nested 'language' select element. unset($element['#options']); /** @var ContentLanguageSettings $default_config */ $default_config = $element['#default_value']; $element['langcode'] = array('#type' => 'select', '#title' => t('Default language'), '#options' => $options + static::getDefaultOptions(), '#description' => t('Explanation of the language options is found on the <a href="@languages_list_page">languages list page</a>.', array('@languages_list_page' => \Drupal::url('entity.configurable_language.collection'))), '#default_value' => $default_config != NULL ? $default_config->getDefaultLangcode() : LanguageInterface::LANGCODE_SITE_DEFAULT); $element['language_alterable'] = array('#type' => 'checkbox', '#title' => t('Show language selector on create and edit pages'), '#default_value' => $default_config != NULL ? $default_config->isLanguageAlterable() : FALSE); // Add the entity type and bundle information to the form if they are set. // They will be used, in the submit handler, to generate the names of the // configuration entities that will store the settings and are a way to uniquely // identify the entity. $language = $form_state->get('language') ?: []; $language += array($element['#name'] => array('entity_type' => $element['#entity_information']['entity_type'], 'bundle' => $element['#entity_information']['bundle'])); $form_state->set('language', $language); // Do not add the submit callback for the language content settings page, // which is handled separately. if ($form['#form_id'] != 'language_content_settings_form') { // Determine where to attach the language_configuration element submit // handler. // @todo Form API: Allow form widgets/sections to declare #submit // handlers. $submit_name = isset($form['actions']['save_continue']) ? 'save_continue' : 'submit'; if (isset($form['actions'][$submit_name]['#submit']) && array_search('language_configuration_element_submit', $form['actions'][$submit_name]['#submit']) === FALSE) { $form['actions'][$submit_name]['#submit'][] = 'language_configuration_element_submit'; } elseif (array_search('language_configuration_element_submit', $form['#submit']) === FALSE) { $form['#submit'][] = 'language_configuration_element_submit'; } } return $element; }
/** * {@inheritdoc} */ public function buildOptionsForm(&$form, FormStateInterface $form_state) { switch ($form_state->get('section')) { case 'test_extender_test_option': $form['#title'] .= $this->t('Test option'); $form['test_extender_test_option'] = array('#title' => $this->t('Test option'), '#type' => 'textfield', '#description' => $this->t('This is a textfield for test_option.'), '#default_value' => $this->options['test_extender_test_option']); } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $configuration = $this->block->getConfiguration(); $this->megaMenu->removeBlock($form_state->get('link'), $configuration['id']); $this->megaMenu->save(); drupal_set_message($this->t('The @label block has been removed.', ['@label' => $configuration['label']])); $form_state->setRedirectUrl($this->getCancelUrl()); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { if ($form_state->get('json')) { $form_state->setResponse(new JsonResponse($form_state->getValues())); } else { $form_state->disableRedirect(); } }
/** * {@inheritdoc} */ protected function getSubmitMessage(FormStateInterface $form_state, $op, $confirm) { $user = $form_state->get('user'); if (\Drupal::currentUser()->id() == $user->id()) { return $this->t('Your newsletter subscriptions have been updated.'); } return $this->t('The newsletter subscriptions for user %account have been updated.', array('%account' => $user->label())); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\Core\Entity\EntityInterface $event */ $event = $form_state->get('event'); $form_state->get('form_display')->extractFormValues($event, $form, $form_state); $event->save(); $t_args = ['%event_label' => $event->label()]; drupal_set_message(t('Event settings updated.', $t_args)); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $params = array('subject' => $this->t('A new message from your website'), 'body' => $form_state->get('text')); $message = \Drupal::service('plugin.manager.mail')->mail('omsu_form', 'notice', '*****@*****.**', $params); if ($message['send']) { drupal_set_message($this->t('Thank you for your message. We will contact you shortly.')); } else { drupal_set_message($this->t('Something went wrong. Please contact the site administrator.')); } }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { module_load_include('inc', 'uc_role', 'uc_role.admin'); if ($form_state->get('step') == UC_FILE_FORM_ACTION) { return array('#validate' => array('uc_file_admin_files_form_action_validate'), '#submit' => array('uc_file_admin_files_form_action_submit')) + $form + uc_file_admin_files_form_action($form, $form_state); } else { // Refresh our file list before display. uc_file_refresh(); return array('#theme' => 'uc_file_admin_files_form_show', '#validate' => array('uc_file_admin_files_form_show_validate'), '#submit' => array('uc_file_admin_files_form_show_submit')) + $form + uc_file_admin_files_form_show_files($form, $form_state); } }
protected function valueForm(&$form, FormStateInterface $form_state) { $users = $this->value ? User::loadMultiple($this->value) : array(); $default_value = EntityAutocomplete::getEntityLabels($users); $form['value'] = array('#type' => 'entity_autocomplete', '#title' => $this->t('Usernames'), '#description' => $this->t('Enter a comma separated list of user names.'), '#target_type' => 'user', '#tags' => TRUE, '#default_value' => $default_value, '#process_default_value' => FALSE); $user_input = $form_state->getUserInput(); if ($form_state->get('exposed') && !isset($user_input[$this->options['expose']['identifier']])) { $user_input[$this->options['expose']['identifier']] = $default_value; $form_state->setUserInput($user_input); } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $config = $this->config('update.settings'); // See if the update_check_disabled setting is being changed, and if so, // invalidate all update status data. if ($form_state->getValue('update_check_disabled') != $config->get('check.disabled_extensions')) { update_storage_clear(); } $config->set('check.disabled_extensions', $form_state->getValue('update_check_disabled'))->set('check.interval_days', $form_state->getValue('update_check_frequency'))->set('notification.emails', $form_state->get('notify_emails'))->set('notification.threshold', $form_state->getValue('update_notification_threshold'))->save(); parent::submitForm($form, $form_state); }
/** * {@inheritdoc} */ protected function valueForm(&$form, FormStateInterface $form_state) { while (is_array($this->value)) { $this->value = $this->value ? array_shift($this->value) : NULL; } $form['value'] = array( '#type' => 'select', '#title' => !$form_state->get('exposed') ? $this->t('Value') : '', '#options' => array(1 => $this->t('True'), 0 => $this->t('False')), '#default_value' => isset($this->value) ? $this->value : '', ); }
/** * Provide a simple textfield for equality */ protected function valueForm(&$form, FormStateInterface $form_state) { $form['value'] = array('#type' => 'textfield', '#title' => $this->t('Value'), '#size' => 30, '#default_value' => $this->value); if ($exposed = $form_state->get('exposed')) { $identifier = $this->options['expose']['identifier']; $user_input = $form_state->getUserInput(); if (!isset($user_input[$identifier])) { $user_input[$identifier] = $this->value; $form_state->setUserInput($user_input); } } }
/** * Submit callback: switch a context to data selecor or direct input mode. */ public function switchContextMode(array &$form, FormStateInterface $form_state) { $element_name = $form_state->getTriggeringElement()['#name']; $mode = $form_state->get($element_name); $switched_mode = $mode == 'selector' ? 'input' : 'selector'; $form_state->set($element_name, $switched_mode); $form_state->setRebuild(); }