本文整理汇总了PHP中Drupal\Core\Form\FormStateInterface::set方法的典型用法代码示例。如果您正苦于以下问题:PHP FormStateInterface::set方法的具体用法?PHP FormStateInterface::set怎么用?PHP FormStateInterface::set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Form\FormStateInterface
的用法示例。
在下文中一共展示了FormStateInterface::set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validateName
/**
* 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('');
}
}
示例2: switchContextMode
/**
* 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();
}
示例3: submitConfigurationForm
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state)
{
$values = $form_state->getValues();
$values['default'] = (bool) $values['default'];
$values['roles'] = array_values(array_filter($values['roles']));
$form_state->set('values', $values);
parent::submitConfigurationForm($form, $form_state);
}
示例4: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
if ($form_state->getTriggeringElement()['#name'] == 'select_id_submit') {
$form_state->set('default_type', $form_state->getValue('id'));
$form_state->setRebuild();
} else {
parent::submitForm($form, $form_state);
}
}
示例5: validateForm
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state)
{
$form_state->set('notify_emails', []);
if (!$form_state->isValueEmpty('update_notify_emails')) {
$valid = array();
$invalid = array();
foreach (explode("\n", trim($form_state->getValue('update_notify_emails'))) as $email) {
$email = trim($email);
if (!empty($email)) {
if ($this->emailValidator->isValid($email)) {
$valid[] = $email;
} else {
$invalid[] = $email;
}
}
}
if (empty($invalid)) {
$form_state->set('notify_emails', $valid);
} elseif (count($invalid) == 1) {
$form_state->setErrorByName('update_notify_emails', $this->t('%email is not a valid email address.', array('%email' => reset($invalid))));
} else {
$form_state->setErrorByName('update_notify_emails', $this->t('%emails are not valid email addresses.', array('%emails' => implode(', ', $invalid))));
}
}
parent::validateForm($form, $form_state);
}
示例6: buildForm
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $mode = 'independent', $bundle = '') {
$form_state->set('target_entity_type_id', $entity_type_id);
$form_state->set('target_bundle', $bundle);
$this->entity->setUpdateEntityType($entity_type_id);
return parent::buildForm($form, $form_state);
}
示例7: buildForm
/**
* {@inheritdoc}
*
* @param string $field_config
* The ID of the field config whose field storage config is being edited.
*/
public function buildForm(array $form, FormStateInterface $form_state, $field_config = NULL)
{
if ($field_config) {
$field = FieldConfig::load($field_config);
$form_state->set('field_config', $field);
$form_state->set('entity_type_id', $field->getTargetEntityTypeId());
$form_state->set('bundle', $field->getTargetBundle());
}
return parent::buildForm($form, $form_state);
}
示例8: buildFieldsForm
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 properties for this index.
$field_options = array('#type' => 'checkboxes', '#title' => $this->t('Contained fields'), '#options' => array(), '#attributes' => array('class' => array('search-api-checkboxes-list')), '#required' => TRUE);
$datasource_labels = $this->getDatasourceLabelPrefixes();
$properties = $this->getAvailableProperties();
ksort($properties);
foreach ($properties as $combined_id => $property) {
list($datasource_id, $name) = Utility::splitCombinedId($combined_id);
$field_options['#options'][$combined_id] = $datasource_labels[$datasource_id] . $property->getLabel();
$field_options[$combined_id] = array('#attributes' => array('title' => $this->t('Machine name: @name', array('@name' => $name))), '#description' => $property->getDescription());
}
$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'] = $field_options;
$form['fields'][$field_id]['fields']['#default_value'] = $field['fields'];
$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')));
}
}
示例9: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $event = NULL)
{
$entity = clone $route_match->getParameter($event);
$form_state->set('event', $entity);
$display = entity_get_form_display($entity->getEntityTypeId(), $entity->bundle(), 'rng_event');
$form_state->set('form_display', $display);
$form['event'] = ['#weight' => 0];
$display->buildForm($entity, $form['event'], $form_state);
$form['actions'] = ['#type' => 'actions'];
$form['actions']['submit'] = ['#type' => 'submit', '#value' => t('Save'), '#button_type' => 'primary'];
return $form;
}
示例10: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$form['title'] = array('#type' => 'textfield', '#title' => 'title', '#default_value' => 'DEFAULT', '#required' => TRUE);
$form_state->set('value', 'State persisted.');
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
return $form;
}
示例11: buildForm
/**
* {@inheritdoc]
*/
public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity_1 = NULL, EntityInterface $entity_2 = NULL)
{
// First entity.
$form_state->set('entity_1', $entity_1);
$form_display_1 = EntityFormDisplay::collectRenderDisplay($entity_1, 'default');
$form_state->set('form_display_1', $form_display_1);
$form_display_1->buildForm($entity_1, $form, $form_state);
// Second entity.
$form_state->set('entity_2', $entity_2);
$form_display_2 = EntityFormDisplay::collectRenderDisplay($entity_2, 'default');
$form_state->set('form_display_2', $form_display_2);
$form['entity_2'] = array('#type' => 'details', '#title' => t('Second entity'), '#tree' => TRUE, '#parents' => array('entity_2'), '#weight' => 50);
$form_display_2->buildForm($entity_2, $form['entity_2'], $form_state);
$form['save'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 100);
return $form;
}
示例12: processLanguageConfiguration
/**
* 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;
}
示例13: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
// Trick inline_entity_form_form_alter() into attaching the handlers,
// WidgetSubmit will be needed once extractFormValues fills the $form_state.
$parents = array_merge($element['#field_parents'], [$items->getName()]);
$ief_id = sha1(implode('-', $parents));
$form_state->set(['inline_entity_form', $ief_id], []);
$element['#type'] = 'fieldset';
$item = $items->get($delta);
if ($item->target_id && !$item->entity) {
$element['warning']['#markup'] = $this->t('Unable to load the referenced entity.');
return $element;
}
$entity = $item->entity;
$op = $entity ? 'edit' : 'add';
$language = $items->getParent()->getValue()->language()->getId();
$parents = array_merge($element['#field_parents'], [$items->getName(), $delta, 'inline_entity_form']);
$bundle = reset($this->getFieldSetting('handler_settings')['target_bundles']);
$element['inline_entity_form'] = $this->getInlineEntityForm($op, $bundle, $language, $delta, $parents, $entity);
if ($op == 'edit') {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
if (!$entity->access('update')) {
// The user isn't allowed to edit the entity, but still needs to see
// it, to be able to reorder values.
$element['entity_label'] = ['#type' => 'markup', '#markup' => $entity->label()];
// Hide the inline form. getInlineEntityForm() still needed to be
// called because otherwise the field re-ordering doesn't work.
$element['inline_entity_form']['#access'] = FALSE;
}
}
return $element;
}
示例14: buildForm
/**
* {@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;
}
示例15: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, LanguageInterface $language = NULL)
{
if ($language) {
$form_state->set('langcode', $language->getId());
}
return parent::buildForm($form, $form_state);
}