本文整理汇总了PHP中Drupal\Core\Form\FormStateInterface::setStorage方法的典型用法代码示例。如果您正苦于以下问题:PHP FormStateInterface::setStorage方法的具体用法?PHP FormStateInterface::setStorage怎么用?PHP FormStateInterface::setStorage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Form\FormStateInterface
的用法示例。
在下文中一共展示了FormStateInterface::setStorage方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
if ($form_state->isRebuilding()) {
$form_state->setUserInput(array());
}
// Initialize
$storage = $form_state->getStorage();
if (empty($storage)) {
$user_input = $form_state->getUserInput();
if (empty($user_input)) {
$_SESSION['constructions'] = 0;
}
// Put the initial thing into the storage
$storage = ['thing' => ['title' => 'none', 'value' => '']];
$form_state->setStorage($storage);
}
// Count how often the form is constructed.
$_SESSION['constructions']++;
drupal_set_message("Form constructions: " . $_SESSION['constructions']);
$form['title'] = array('#type' => 'textfield', '#title' => 'Title', '#default_value' => $storage['thing']['title'], '#required' => TRUE);
$form['value'] = array('#type' => 'textfield', '#title' => 'Value', '#default_value' => $storage['thing']['value'], '#element_validate' => array('::elementValidateValueCached'));
$form['continue_button'] = array('#type' => 'button', '#value' => 'Reset');
$form['continue_submit'] = array('#type' => 'submit', '#value' => 'Continue submit', '#submit' => array('::continueSubmitForm'));
$form['submit'] = array('#type' => 'submit', '#value' => 'Save');
if (\Drupal::request()->get('cache')) {
// Manually activate caching, so we can test that the storage keeps working
// when it's enabled.
$form_state->setCached();
}
if ($this->getRequest()->get('immutable')) {
$form_state->addBuildInfo('immutable', TRUE);
}
return $form;
}
示例2: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
if ($form_state->isRebuilding()) {
$form_state->setUserInput(array());
}
// Initialize
$storage = $form_state->getStorage();
if (empty($storage)) {
$user_input = $form_state->getUserInput();
if (empty($user_input)) {
$_SESSION['constructions'] = 0;
}
// Put the initial thing into the storage
$storage = ['thing' => ['title' => 'none', 'value' => '']];
$form_state->setStorage($storage);
}
// Count how often the form is constructed.
$_SESSION['constructions']++;
drupal_set_message("Form constructions: " . $_SESSION['constructions']);
$form['title'] = array('#type' => 'textfield', '#title' => 'Title', '#default_value' => $storage['thing']['title'], '#required' => TRUE);
$form['value'] = array('#type' => 'textfield', '#title' => 'Value', '#default_value' => $storage['thing']['value'], '#element_validate' => array('::elementValidateValueCached'));
$form['continue_button'] = array('#type' => 'button', '#value' => 'Reset');
$form['continue_submit'] = array('#type' => 'submit', '#value' => 'Continue submit', '#submit' => array('::continueSubmitForm'));
$form['submit'] = array('#type' => 'submit', '#value' => 'Save');
// @todo Remove this in https://www.drupal.org/node/2524408, because form
// cache immutability is no longer necessary, because we no longer cache
// forms during safe HTTP methods. In the meantime, because
// Drupal\system\Tests\Form still has test coverage for a poisoned form
// cache following a GET request, trick $form_state into caching the form
// to keep that test working until we either remove it or change it in
// that issue.
if ($this->getRequest()->get('immutable')) {
$form_state->addBuildInfo('immutable', TRUE);
if ($this->getRequest()->get('cache') && $this->getRequest()->isMethodSafe()) {
$form_state->setRequestMethod('FAKE');
$form_state->setCached();
}
}
return $form;
}
示例3: automaticStartSubmit
/**
* @param $form_state
*/
protected function automaticStartSubmit(FormStateInterface &$form_state)
{
$config = $this->config('acquia_connector.settings');
$storage = $form_state->getStorage();
if (empty($storage['response']['subscription'])) {
drupal_set_message($this->t('No subscriptions were found for your account.'), 'error');
} elseif (count($storage['response']['subscription']) > 1) {
// Multistep form for choosing from available subscriptions.
$storage['choose'] = TRUE;
// Force rebuild with next step.
$form_state->setRebuild(TRUE);
$form_state->setStorage($storage);
} else {
// One subscription so set id/key pair.
$sub = $storage['response']['subscription'][0];
$config->set('key', $sub['key'])->set('identifier', $sub['identifier'])->set('subscription_name', $sub['name'])->save();
}
}
示例4: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
// Honeypot Configuration.
$form['configuration'] = ['#type' => 'fieldset', '#title' => t('Honeypot Configuration'), '#collapsible' => TRUE, '#collapsed' => FALSE];
$form['configuration']['protect_all_forms'] = ['#type' => 'checkbox', '#title' => t('Protect all forms with Honeypot'), '#description' => t('Enable Honeypot protection for ALL forms on this site (it is best to only enable Honeypot for the forms you need below).'), '#default_value' => $this->config('honeypot.settings')->get('protect_all_forms')];
$form['configuration']['protect_all_forms']['#description'] .= '<br />' . t('<strong>Page caching will be disabled on any page where a form is present if the Honeypot time limit is not set to 0.</strong>');
$form['configuration']['log'] = ['#type' => 'checkbox', '#title' => t('Log blocked form submissions'), '#description' => t('Log submissions that are blocked due to Honeypot protection.'), '#default_value' => $this->config('honeypot.settings')->get('log')];
$form['configuration']['element_name'] = ['#type' => 'textfield', '#title' => t('Honeypot element name'), '#description' => t("The name of the Honeypot form field. It's usually most effective to use a generic name like email, homepage, or link, but this should be changed if it interferes with fields that are already in your forms. Must not contain spaces or special characters."), '#default_value' => $this->config('honeypot.settings')->get('element_name'), '#required' => TRUE, '#size' => 30];
$form['configuration']['time_limit'] = ['#type' => 'textfield', '#title' => t('Honeypot time limit'), '#description' => t('Minimum time required before form should be considered entered by a human instead of a bot. Set to 0 to disable.'), '#default_value' => $this->config('honeypot.settings')->get('time_limit'), '#required' => TRUE, '#size' => 5, '#field_suffix' => t('seconds')];
$form['configuration']['time_limit']['#description'] .= '<br />' . t('<strong>Page caching will be disabled if there is a form protected by time limit on the page.</strong>');
// Honeypot Enabled forms.
$form_settings = $this->config('honeypot.settings')->get('form_settings');
$form['form_settings'] = ['#type' => 'fieldset', '#title' => t('Honeypot Enabled Forms'), '#description' => t("Check the boxes next to individual forms on which you'd like Honeypot protection enabled."), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, '#states' => ['invisible' => ['input[name="protect_all_forms"]' => ['checked' => TRUE]]]];
// Generic forms.
$form['form_settings']['general_forms'] = ['#markup' => '<h5>' . t('General Forms') . '</h5>'];
// User register form.
$form['form_settings']['user_register_form'] = ['#type' => 'checkbox', '#title' => t('User Registration form'), '#default_value' => $this->getFormSettingsValue($form_settings, 'user_register_form')];
// User password form.
$form['form_settings']['user_pass'] = ['#type' => 'checkbox', '#title' => t('User Password Reset form'), '#default_value' => $this->getFormSettingsValue($form_settings, 'user_pass')];
// If webform.module enabled, add webforms.
// TODO D8 - See if D8 version of Webform.module still uses this form ID.
if (\Drupal::moduleHandler()->moduleExists('webform')) {
$form['form_settings']['webforms'] = ['#type' => 'checkbox', '#title' => t('Webforms (all)'), '#default_value' => $this->getFormSettingsValue($form_settings, 'webforms')];
}
// If contact.module enabled, add contact forms.
if (\Drupal::moduleHandler()->moduleExists('contact')) {
$form['form_settings']['contact_forms'] = ['#markup' => '<h5>' . t('Contact Forms') . '</h5>'];
$bundles = \Drupal::entityManager()->getBundleInfo('contact_message');
$formController = \Drupal::entityManager()->getFormObject('contact_message', 'default');
foreach ($bundles as $bundle_key => $bundle) {
$stub = entity_create('contact_message', ['contact_form' => $bundle_key]);
$formController->setEntity($stub);
$form_id = $formController->getFormId();
$form['form_settings'][$form_id] = ['#type' => 'checkbox', '#title' => SafeMarkup::checkPlain($bundle['label']), '#default_value' => $this->getFormSettingsValue($form_settings, $form_id)];
}
}
// Node types for node forms.
if (\Drupal::moduleHandler()->moduleExists('node')) {
$types = NodeType::loadMultiple();
if (!empty($types)) {
// Node forms.
$form['form_settings']['node_forms'] = ['#markup' => '<h5>' . t('Node Forms') . '</h5>'];
foreach ($types as $type) {
$id = $type->getEntityTypeId() . '_node_form';
$form['form_settings'][$id] = ['#type' => 'checkbox', '#title' => t('@name node form', ['@name' => $type->label()]), '#default_value' => $this->getFormSettingsValue($form_settings, $id)];
}
}
}
// Comment types for comment forms.
if (\Drupal::moduleHandler()->moduleExists('comment')) {
$types = CommentType::loadMultiple();
if (!empty($types)) {
$form['form_settings']['comment_forms'] = ['#markup' => '<h5>' . t('Comment Forms') . '</h5>'];
foreach ($types as $type) {
$id = 'comment_' . $type->id() . '_form';
$form['form_settings'][$id] = ['#type' => 'checkbox', '#title' => t('@name comment form', ['@name' => $type->label()]), '#default_value' => $this->getFormSettingsValue($form_settings, $id)];
}
}
}
// Store the keys we want to save in configuration when form is submitted.
$keys_to_save = array_keys($form['configuration']);
foreach ($keys_to_save as $key => $key_to_save) {
if (strpos($key_to_save, '#') !== FALSE) {
unset($keys_to_save[$key]);
}
}
$form_state->setStorage(['keys' => $keys_to_save]);
// For now, manually add submit button. Hopefully, by the time D8 is
// released, there will be something like system_settings_form() in D7.
$form['actions']['#type'] = 'container';
$form['actions']['submit'] = ['#type' => 'submit', '#value' => t('Save configuration')];
return $form;
}
示例5: buildConfirmForm
/**
* Build the form gathering database credential and file location information.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form structure.
*/
public function buildConfirmForm(array $form, FormStateInterface $form_state)
{
$rollback = $form_state->getValue('upgrade_option') == static::MIGRATE_UPGRADE_ROLLBACK;
if ($rollback) {
$form_state->setStorage(['upgrade_option' => static::MIGRATE_UPGRADE_ROLLBACK]);
}
$form['#title'] = $this->getQuestion();
$form['#attributes']['class'][] = 'confirmation';
$form[$this->getFormName()] = ['#type' => 'hidden', '#value' => 1];
if ($rollback) {
$form['rollback'] = ['#markup' => $this->t('All previously-imported content, as well as configuration such as field definitions, will be removed.')];
} else {
$table_data = [];
$system_data = [];
foreach ($form_state->get('migration_ids') as $migration_id) {
/** @var MigrationInterface $migration */
$migration = Migration::load($migration_id);
// Fetch the system data at the first opportunity.
if (empty($system_data) && is_a($migration->getSourcePlugin(), '\\Drupal\\migrate_drupal\\Plugin\\migrate\\source\\DrupalSqlBase')) {
$system_data = $migration->getSourcePlugin()->getSystemData();
}
$template_id = $migration->get('template');
$source_module = $this->moduleUpgradePaths[$template_id]['source_module'];
$destination_module = $this->moduleUpgradePaths[$template_id]['destination_module'];
$table_data[$source_module][$destination_module][$migration_id] = $migration->label();
}
ksort($table_data);
foreach ($table_data as $source_module => $destination_module_info) {
ksort($table_data[$source_module]);
}
$unmigrated_source_modules = array_diff_key($system_data['module'], $table_data);
// Missing migrations.
$desc = "The following items will not be upgraded. " . 'For more information see <a href="https://www.drupal.org/upgrade/migrate"> Upgrading from Drupal 6 or 7 to Drupal 8</a>.';
$form['missing_module_list_title'] = ['#type' => 'item', '#title' => t('Missing upgrade paths'), '#description' => $this->t($desc)];
$form['missing_module_list'] = ['#type' => 'table', '#header' => [$this->t('Source'), $this->t('Destination')]];
$missing_count = 0;
ksort($unmigrated_source_modules);
foreach ($unmigrated_source_modules as $source_module => $module_data) {
if ($module_data['status']) {
$missing_count++;
$form['missing_module_list'][$source_module] = ['source_module' => ['#plain_text' => $source_module], 'destination_module' => ['#plain_text' => 'Missing']];
}
}
// Available migrations.
$form['available_module_list'] = ['#tree' => TRUE, '#type' => 'details', '#title' => t('Available upgrade paths')];
$form['available_module_list']['module_list'] = ['#type' => 'table', '#header' => [$this->t('Source'), $this->t('Destination')]];
$available_count = 0;
foreach ($table_data as $source_module => $destination_module_info) {
$available_count++;
$destination_details = [];
foreach ($destination_module_info as $destination_module => $migration_ids) {
$destination_details[$destination_module] = ['#type' => 'item', '#plain_text' => t($destination_module)];
}
$form['available_module_list']['module_list'][$source_module] = ['source_module' => ['#plain_text' => $source_module], 'destination_module' => $destination_details];
}
$form['counts'] = ['#type' => 'item', '#title' => "<ul><li>" . t($available_count . ' available upgrade paths') . "</li><li>" . t($missing_count . ' missing upgrade paths') . "</li></ul>", '#weight' => -15];
}
if ($rollback) {
$confirm_text = $this->t('Perform rollback');
} else {
$confirm_text = $this->t('Perform upgrade');
}
$form['actions'] = ['#type' => 'actions'];
$form['actions']['submit'] = ['#type' => 'submit', '#value' => $confirm_text, '#button_type' => 'primary', '#validate' => [], '#submit' => ['::submitConfirmForm']];
$form['actions']['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest());
// By default, render the form using theme_confirm_form().
if (!isset($form['#theme'])) {
$form['#theme'] = 'confirm_form';
}
return $form;
}
示例6: buildForm
//.........这里部分代码省略.........
}
}
// Generate conditional topological order & report any errors.
$sorter = webform_get_conditional_sorter($node);
$sorter->reportErrors();
// Execute the conditionals on the current input values
$input_values = $sorter->executeConditionals($input_values);
// Allow values from other pages to be sent to browser for conditionals.
$form['#conditional_values'] = $input_values;
// Allow components access to most up-to-date values.
$form_state['#conditional_values'] = $input_values;
// For resuming a previous draft, find the next page after the last
// validated page.
if (!isset($form_state->getStorage()['page_num']) && $submission && $submission->is_draft && $submission->highest_valid_page) {
// Find the
// 1) previous/next non-empty page, or
// 2) the preview page, or
// 3) the preview page, forcing its display if the form would
// unexpectedly submit, or
// 4) page 1 even if empty, if no other previous page would be shown
$form_state->set(['webform', 'page_num'], $submission->highest_valid_page);
do {
$form_state->set(['webform', 'page_num'], $form_state->get(['webform', 'page_num']) + 1);
} while (!webform_get_conditional_sorter($node)->pageVisibility($form_state->get(['webform', 'page_num'])));
if (!$form_state->get(['webform', 'preview']) && $form_state->get(['webform', 'page_num']) == $form_state->get(['webform', 'page_count']) + (int) (!$form_state->get(['webform', 'preview']))) {
// Force a preview to avert an unintended submission via Next.
$form_state->set(['webform', 'preview'], TRUE);
$form_state->set(['webform', 'page_count'], $form_state->get(['webform', 'page_count']) + 1);
}
// The form hasn't been submitted (ever) and the preview code will
// expect $form_state['values']['submitted'] to be set from a previous
// submission, so provide these values here.
$form_state->setValue('submitted', $input_values);
$form_state->setStorage(['submitted' => $input_values]);
}
// Shorten up our variable names.
$component_tree = $form_state->get(['webform', 'component_tree']);
$page_count = $form_state->get(['webform', 'page_count']);
$page_num = $form_state->get(['webform', 'page_num']);
$preview = $form_state->get(['webform', 'preview']);
if ($page_count > 1) {
$page_labels = webform_page_labels($node, $form_state);
$form['progressbar'] = ['#theme' => 'webform_progressbar', '#node' => $node, '#page_num' => $page_num, '#page_count' => count($page_labels), '#page_labels' => $page_labels, '#weight' => -100];
}
// Check whether a previous submission was truncated. The length of the
// client form is not estimated before submission because a) the
// determination may not be accurate for some webform components and b)
// the error will be apparent upon submission.
webform_input_vars_check($form, $form_state, 'submitted');
// Recursively add components to the form. The unfiltered version of the
// form (typically used in Form Builder), includes all components.
foreach ($component_tree['children'] as $cid => $component) {
if ($component['type'] == 'pagebreak') {
$next_page_labels[$component['page_num'] - 1] = !empty($component['extra']['next_page_label']) ? $component['extra']['next_page_label'] : $this->t('Next Page >');
$prev_page_labels[$component['page_num']] = !empty($component['extra']['prev_page_label']) ? $component['extra']['prev_page_label'] : $this->t('< Previous Page');
}
if (!$filter || $sorter->componentVisibility($cid, $page_num)) {
$component_value = isset($input_values[$cid]) ? $input_values[$cid] : NULL;
_webform_client_form_add_component($node, $component, $component_value, $form['submitted'], $form, $input_values, 'form', $page_num, $filter);
}
}
if ($preview) {
$next_page_labels[$page_count - 1] = $node->webform['preview_next_button_label'] ? $node->webform['preview_next_button_label'] : $this->t('Preview');
$prev_page_labels[$page_count] = $node->webform['preview_prev_button_label'] ? $node->webform['preview_prev_button_label'] : $this->t('< Previous');
}
// Add the preview if needed.
示例7: setStorage
/**
* {@inheritdoc}
*/
public function setStorage(array $storage)
{
$this->decoratedFormState->setStorage($storage);
return $this;
}
示例8: testSetStorage
/**
* @covers ::setStorage
*/
public function testSetStorage()
{
$storage = ['FOO' => 'BAR'];
$this->decoratedFormState->setStorage($storage)->shouldBeCalled();
$this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setStorage($storage));
}