/** * {@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; }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { if ($this->getRequest()->get('cache')) { // Manually activate caching, so we can test that the storage keeps working // when it's enabled. $form_state->setCached(); } }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { // Test using form cache when re-displaying a form due to validation // errors. if ($form_state->hasAnyErrors()) { $form_state->setCached(); } }
/** * {@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 buildForm(array $form, FormStateInterface $form_state) { $form['title'] = array('#type' => 'textfield', '#title' => 'Title', '#required' => TRUE); $form['test_build_id_old'] = array('#type' => 'item', '#title' => 'Old build id', '#markup' => 'No old build id'); $form['submit'] = array('#type' => 'submit', '#value' => 'Save'); $form['rebuild'] = array('#type' => 'submit', '#value' => 'Rebuild', '#submit' => array(array($this, 'form_test_storage_page_cache_rebuild'))); $form['#after_build'] = array(array($this, 'form_test_storage_page_cache_old_build_id')); $form_state->setCached(); return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $object = new Callbacks(); $form['name'] = array('#type' => 'textfield', '#title' => 'Name', '#default_value' => '', '#element_validate' => array(array($object, 'validateName'))); $form['submit'] = array('#type' => 'submit', '#value' => 'Save'); // To simplify this test, enable form caching and use form storage to // remember our alteration. $form_state->setCached(); return $form; }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { if ($form_state->getValue('name') == 'validate') { // Alter the form element. $form['name']['#value'] = '#value changed by #validate'; // Alter the submitted value in $form_state. $form_state->setValueForElement($form['name'], 'value changed by setValueForElement() in #validate'); // Output the element's value from $form_state. drupal_set_message(t('@label value: @value', array('@label' => $form['name']['#title'], '@value' => $form_state->getValue('name')))); // Trigger a form validation error to see our changes. $form_state->setErrorByName(''); // To simplify this test, enable form caching and use form storage to // remember our alteration. $form_state->setCached(); } }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { /* * The #ajax attribute used in the temperature input element defines an ajax * callback that will invoke the colorCallback method on this form object. * Whenever the temperature element changes, it will invoke this callback * and replace the contents of the color_wrapper container with the results of this * method call. */ $form['temperature'] = ['#title' => $this->t('Temperature'), '#type' => 'select', '#options' => ['warm' => 'Warm', 'cool' => 'Cool'], '#empty_option' => $this->t('-select'), '#ajax' => ['callback' => '::colorCallback', 'wrapper' => 'color-wrapper']]; // Disable caching on this form. $form_state->setCached(FALSE); $form['actions'] = ['#type' => 'actions']; // Add a submit button that handles the submission of the form. $form['actions']['submit'] = ['#type' => 'submit', '#value' => $this->t('Submit')]; $form['color_wrapper'] = ['#type' => 'container', '#attributes' => ['id' => 'color-wrapper']]; return $form; }
/** * @param array $form * @param \Drupal\Core\Form\FormStateInterface $form_state * @param \Drupal\taxonomy\VocabularyInterface $vocabulary * @return array */ public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL, $parents = array()) { // Cache form state so that we keep the parents in the modal dialog. // For non modals (non POST request), form state caching on is not allowed. // @see FormState::setCached() if ($this->getRequest()->getMethod() == 'POST') { $form_state->setCached(TRUE); } $form['voc'] = array('#type' => 'value', '#value' => $taxonomy_vocabulary); $form['parents']['#tree'] = TRUE; foreach ($parents as $p) { $form['parents'][$p] = array('#type' => 'value', '#value' => $p); } $description = $this->t("If you have selected one or more terms in the tree view, the new terms are automatically children of those."); $form['help'] = array('#markup' => $description); $form['mass_add'] = array('#type' => 'textarea', '#title' => $this->t('Terms'), '#description' => $this->t("One term per line. Child terms can be prefixed with a\n dash '-' (one dash per hierarchy level). Terms that should not become\n child terms and start with a dash need to be wrapped in double quotes.\n <br />Example:<br />\n animals<br />\n -canine<br />\n --dog<br />\n --wolf<br />\n -feline<br />\n --cat"), '#rows' => 10, '#required' => TRUE); $form['add'] = array('#type' => 'submit', '#value' => $this->t('Add')); return $form; }
/** * {@inheritdoc} * * @param \Drupal\filter\Entity\FilterFormat $filter_format * The filter format for which this dialog corresponds. */ public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) { // This form is special, in that the default values do not come from the // server side, but from the client side, from a text editor. We must cache // this data in form state, because when the form is rebuilt, we will be // receiving values from the form, instead of the values from the text // editor. If we don't cache it, this data will be lost. if (isset($form_state->getUserInput()['editor_object'])) { // By convention, the data that the text editor sends to any dialog is in // the 'editor_object' key. And the image dialog for text editors expects // that data to be the attributes for an <img> element. $file_element = $form_state->getUserInput()['editor_object']; $form_state->set('file_element', $file_element); $form_state->setCached(TRUE); } else { // Retrieve the image element's attributes from form state. $file_element = $form_state->get('file_element') ?: []; } $form['#tree'] = TRUE; $form['#attached']['library'][] = 'editor/drupal.editor.dialog'; $form['#prefix'] = '<div id="editor-file-dialog-form">'; $form['#suffix'] = '</div>'; // Load dialog settings. $editor = editor_load($filter_format->id()); $file_upload = $editor->getThirdPartySettings('editor_file'); $max_filesize = min(Bytes::toInt($file_upload['max_size']), file_upload_max_size()); $existing_file = isset($file_element['data-entity-uuid']) ? \Drupal::entityManager()->loadEntityByUuid('file', $file_element['data-entity-uuid']) : NULL; $fid = $existing_file ? $existing_file->id() : NULL; $form['fid'] = array('#title' => $this->t('File'), '#type' => 'managed_file', '#upload_location' => $file_upload['scheme'] . '://' . $file_upload['directory'], '#default_value' => $fid ? array($fid) : NULL, '#upload_validators' => array('file_validate_extensions' => !empty($file_upload['extensions']) ? array($file_upload['extensions']) : array('txt'), 'file_validate_size' => array($max_filesize)), '#required' => TRUE); $form['attributes']['href'] = array('#title' => $this->t('URL'), '#type' => 'textfield', '#default_value' => isset($file_element['href']) ? $file_element['href'] : '', '#maxlength' => 2048, '#required' => TRUE); if ($file_upload['status']) { $form['attributes']['href']['#access'] = FALSE; $form['attributes']['href']['#required'] = FALSE; } else { $form['fid']['#access'] = FALSE; $form['fid']['#required'] = FALSE; } $form['actions'] = array('#type' => 'actions'); $form['actions']['save_modal'] = array('#type' => 'submit', '#value' => $this->t('Save'), '#submit' => array(), '#ajax' => array('callback' => '::submitForm', 'event' => 'click')); return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL, $selected_terms = array()) { if (empty($selected_terms)) { $form['info'] = array('#markup' => $this->t('Please select the terms you want to export.')); return $form; } // Cache form state so that we keep the parents in the modal dialog. $form_state->setCached(TRUE); $form['voc'] = array('#type' => 'value', '#value' => $taxonomy_vocabulary); $form['selected_terms']['#tree'] = TRUE; $items = array(); foreach ($selected_terms as $t) { $term = $this->termStorage->load($t); $items[] = $term->getName(); $form['selected_terms'][$t] = array('#type' => 'value', '#value' => $t); } $form['terms'] = array('#theme' => 'item_list', '#items' => $items, '#title' => $this->t('Selected terms for export:')); $form['download_csv'] = array('#type' => 'submit', '#value' => $this->t('Download CSV')); $form['export'] = array('#type' => 'submit', '#value' => $this->t('Export')); return $form; }
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL, $selected_terms = array()) { if (empty($selected_terms)) { $form['info'] = array('#markup' => $this->t('Please select the terms you want to move.')); return $form; } // Cache form state so that we keep the parents in the modal dialog. $form_state->setCached(TRUE); $form['voc'] = array('#type' => 'value', '#value' => $taxonomy_vocabulary); $form['selected_terms']['#tree'] = TRUE; $items = array(); foreach ($selected_terms as $t) { $term = $this->termStorage->load($t); $items[] = $term->getName(); $form['selected_terms'][$t] = array('#type' => 'value', '#value' => $t); } $form['terms'] = array('#theme' => 'item_list', '#items' => $items, '#title' => $this->t('Selected terms to move:')); // @todo Add autocomplete to select/add parent term. $form['keep_old_parents'] = array('#type' => 'checkbox', '#title' => $this->t('Keep old parents and add new ones (multi-parent). Otherwise old parents get replaced.')); $form['delete'] = array('#type' => 'submit', '#value' => $this->t('Move')); return $form; }
/** * Form element processing handler for the #ajax form property. * * This method is useful for non-input elements that can be used in and * outside the context of a form. * * @param array $element * An associative array containing the properties of the element. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * @param array $complete_form * The complete form structure. * * @return array * The processed element. * * @see self::preRenderAjaxForm() */ public static function processAjaxForm(&$element, FormStateInterface $form_state, &$complete_form) { $element = static::preRenderAjaxForm($element); // If the element was processed as an #ajax element, and a custom URL was // provided, set the form to be cached. if (!empty($element['#ajax_processed']) && !empty($element['#ajax']['url'])) { $form_state->setCached(); } return $element; }
/** * @covers ::setCached * * @dataProvider providerSingleBooleanArgument * * @param bool $cache * * @expectedException \LogicException */ public function testSetCachedWithLogicException($cache) { $this->decoratedFormState->setCached($cache)->willThrow(\LogicException::class); $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setCached($cache)); }
/** * {@inheritdoc} */ public function setCached($cache = TRUE) { $this->decoratedFormState->setCached($cache); return $this; }
/** * {@inheritdoc} * * @param \Drupal\filter\Entity\FilterFormat $filter_format * The filter format for which this dialog corresponds. */ public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) { // This form is special, in that the default values do not come from the // server side, but from the client side, from a text editor. We must cache // this data in form state, because when the form is rebuilt, we will be // receiving values from the form, instead of the values from the text // editor. If we don't cache it, this data will be lost. $user_input = $form_state->getUserInput(); if (isset($user_input['editor_object'])) { // By convention, the data that the text editor sends to any dialog is in // the 'editor_object' key. And the image dialog for text editors expects // that data to be the attributes for an <img> element. $image_element = $user_input['editor_object']; $form_state->set('image_element', $image_element); $form_state->setCached(TRUE); } elseif ($form_state->getTemporaryValue('wizard')) { $image_element = $form_state->getTemporaryValue('wizard')['image_element']; } else { // Retrieve the image element's attributes from form state. $image_element = $form_state->get('image_element') ?: []; } // Add libraries and wrap the form in ajax wrappers. $form['#tree'] = TRUE; $form['#attached']['library'][] = 'editor/drupal.editor.dialog'; $form['#prefix'] = '<div id="' . self::AJAX_WRAPPER_ID . '">'; $form['#suffix'] = '</div>'; /** @var \Drupal\editor\Entity\Editor $editor */ $editor = $this->entityTypeManager->getStorage('editor')->load($filter_format->id()); // Construct strings to use in the upload validators. $embridge_image_settings = $editor->getSettings()['plugins']['embridgeimage']['embridge_image_upload']; $max_filesize = min(Bytes::toInt($embridge_image_settings['max_size']), file_upload_max_size()); /** @var \Drupal\embridge\EmbridgeAssetEntityInterface $existing_asset */ $existing_asset = isset($image_element['data-entity-uuid']) ? $this->entityRepository->loadEntityByUuid('embridge_asset_entity', $image_element['data-entity-uuid']) : NULL; $asset_id = $existing_asset ? $existing_asset->id() : NULL; /** @var \Drupal\embridge\EmbridgeCatalogInterface $catalog */ $catalog = $this->entityTypeManager->getStorage('embridge_catalog')->load($embridge_image_settings['catalog_id']); // Create a preview image. $preview = FALSE; if (!empty($user_input['_triggering_element_name'])) { $triggering_element = $user_input['_triggering_element_name']; } // If we are editing an existing asset, use that thumbnail. if (empty($form_state->getValues()) && $existing_asset) { $preview = $this->assetHelper->getAssetConversionUrl($existing_asset, $catalog->getApplicationId(), 'thumb'); } elseif (isset($triggering_element) && $triggering_element != 'asset_remove_button' && ($uploaded_id = $form_state->getValue(['asset', 0]))) { /** @var \Drupal\embridge\EmbridgeAssetEntityInterface $uploaded_asset */ $uploaded_asset = $this->entityTypeManager->getStorage('embridge_asset_entity')->load($uploaded_id); if ($uploaded_asset) { $preview = $this->assetHelper->getAssetConversionUrl($uploaded_asset, $catalog->getApplicationId(), 'thumb'); } } // Use a stock image for preview. if (!$preview) { $preview = drupal_get_path('module', 'embridge_ckeditor') . '/images/preview-image.png'; } // TODO: Make this configurable. $allowed_extensions = 'gif png jpg jpeg'; $url_options = ['filter_format' => $filter_format->id(), 'extensions' => $allowed_extensions, 'catalog_id' => $embridge_image_settings['catalog_id']]; $link_url = Url::fromRoute('embridge_ckeditor.image.wizard', $url_options); $link_url->setOptions(['attributes' => ['class' => ['use-ajax', 'button'], 'data-accepts' => 'application/vnd.drupal-modal', 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode(['width' => 1000])]]); $class = get_class($this); $form['asset'] = ['preview' => ['#theme' => 'image', '#uri' => $preview, '#weight' => -100], '#title' => $this->t('Image'), '#type' => 'embridge_asset', '#catalog_id' => $embridge_image_settings['catalog_id'], '#library_id' => $embridge_image_settings['library_id'], '#upload_location' => 'public://' . $embridge_image_settings['directory'], '#default_value' => $asset_id ? [$asset_id] : NULL, '#upload_validators' => ['validateFileExtensions' => [$allowed_extensions], 'validateFileSize' => [$max_filesize]], '#pre_render' => [[$class, 'preRenderAssetElement']], '#allow_search' => FALSE, '#required' => TRUE, 'search_link' => Link::fromTextAndUrl('Search asset library', $link_url)->toRenderable()]; $form['asset']['search_link']['#weight'] = 100; $form['attributes'] = ['#type' => 'container', '#tree' => TRUE, '#attributes' => ['class' => ['image-attributes']]]; $form['attributes']['src'] = ['#type' => 'value']; $alt = isset($image_element['alt']) ? $image_element['alt'] : ''; $form['attributes']['alt'] = ['#title' => $this->t('Alternative text'), '#description' => $this->t('The alt text describes the image for non-sighted users. <br/>The alt text can remain empty only if the image conveys no meaning (is decorative only).'), '#type' => 'textfield', '#default_value' => $alt, '#maxlength' => 2048]; $conversion = isset($image_element['data-conversion']) ? $image_element['data-conversion'] : ''; $conversions_array = $catalog->getConversionsArray(); $form['attributes']['data-conversion'] = ['#title' => $this->t('Image size'), '#description' => $this->t('Choose the image size conversion to display.'), '#type' => 'select', '#default_value' => $conversion, '#options' => array_combine($conversions_array, $conversions_array)]; // When Drupal core's filter_align is being used, the text editor may // offer the ability to change the alignment. if ($filter_format->filters('filter_align')->status) { $data_align = !empty($image_element['data-align']) ? $image_element['data-align'] : ''; $form['attributes']['data-align'] = ['#title' => $this->t('Align'), '#description' => $this->t('How the image will align within the content.'), '#type' => 'select', '#options' => ['none' => $this->t('None'), 'left' => $this->t('Left'), 'center' => $this->t('Center'), 'right' => $this->t('Right')], '#default_value' => $data_align]; } $form['actions'] = ['#type' => 'actions']; $form['actions']['save_modal'] = ['#type' => 'submit', '#value' => $this->t('Save'), '#submit' => [], '#ajax' => ['callback' => [$this, 'ajaxSave'], 'event' => 'click'], '#attributes' => ['class' => ['button--primary']]]; return $form; }
/** * {@inheritdoc} */ public function setCached($cache = TRUE) { $this->mainFormState->setCached($cache); return $this; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, ViewExecutable $view = NULL, $output = []) { if (!($step = $form_state->get('step'))) { $step = 'views_form_views_form'; $form_state->set('step', $step); } $form_state->set(['step_controller', 'views_form_views_form'], 'Drupal\\views\\Form\\ViewsFormMainForm'); // Cache the built form to prevent it from being rebuilt prior to validation // and submission, which could lead to data being processed incorrectly, // because the views rows (and thus, the form elements as well) have changed // in the meantime. $form_state->setCached(); $form = array(); $query = $this->requestStack->getCurrentRequest()->query->all(); $query = UrlHelper::filterQueryParameters($query, array(), ''); $options = array('query' => $query); $form['#action'] = $view->hasUrl() ? $view->getUrl()->setOptions($options)->toString() : Url::fromRoute('<current>')->setOptions($options)->toString(); // Tell the preprocessor whether it should hide the header, footer, pager... $form['show_view_elements'] = array('#type' => 'value', '#value' => $step == 'views_form_views_form' ? TRUE : FALSE); $form_object = $this->getFormObject($form_state); $form += $form_object->buildForm($form, $form_state, $view, $output); return $form; }
/** * Form element processing handler for the #ajax form property. * * This method is useful for non-input elements that can be used in and * outside the context of a form. * * @param array $element * An associative array containing the properties of the element. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * @param array $complete_form * The complete form structure. * * @return array * The processed element. * * @see self::preRenderAjaxForm() */ public static function processAjaxForm(&$element, FormStateInterface $form_state, &$complete_form) { $element = static::preRenderAjaxForm($element); if (!empty($element['#ajax_processed'])) { $form_state->setCached(); } return $element; }
/** * {@inheritdoc} * * @param \Drupal\editor\Entity\Editor $editor * The text editor to which this dialog corresponds. */ public function buildForm(array $form, FormStateInterface $form_state, Editor $editor = NULL) { // This form is special, in that the default values do not come from the // server side, but from the client side, from a text editor. We must cache // this data in form state, because when the form is rebuilt, we will be // receiving values from the form, instead of the values from the text // editor. If we don't cache it, this data will be lost. if (isset($form_state->getUserInput()['editor_object'])) { // By convention, the data that the text editor sends to any dialog is in // the 'editor_object' key. And the image dialog for text editors expects // that data to be the attributes for an <img> element. $image_element = $form_state->getUserInput()['editor_object']; $form_state->set('image_element', $image_element); $form_state->setCached(TRUE); } else { // Retrieve the image element's attributes from form state. $image_element = $form_state->get('image_element') ?: []; } $form['#tree'] = TRUE; $form['#attached']['library'][] = 'editor/drupal.editor.dialog'; $form['#prefix'] = '<div id="editor-image-dialog-form">'; $form['#suffix'] = '</div>'; // Construct strings to use in the upload validators. $image_upload = $editor->getImageUploadSettings(); if (!empty($image_upload['max_dimensions']['width']) || !empty($image_upload['max_dimensions']['height'])) { $max_dimensions = $image_upload['max_dimensions']['width'] . 'x' . $image_upload['max_dimensions']['height']; } else { $max_dimensions = 0; } $max_filesize = min(Bytes::toInt($image_upload['max_size']), file_upload_max_size()); $existing_file = isset($image_element['data-entity-uuid']) ? \Drupal::entityManager()->loadEntityByUuid('file', $image_element['data-entity-uuid']) : NULL; $fid = $existing_file ? $existing_file->id() : NULL; $form['fid'] = array('#title' => $this->t('Image'), '#type' => 'managed_file', '#upload_location' => $image_upload['scheme'] . '://' . $image_upload['directory'], '#default_value' => $fid ? array($fid) : NULL, '#upload_validators' => array('file_validate_extensions' => array('gif png jpg jpeg'), 'file_validate_size' => array($max_filesize), 'file_validate_image_resolution' => array($max_dimensions)), '#required' => TRUE); $form['attributes']['src'] = array('#title' => $this->t('URL'), '#type' => 'textfield', '#default_value' => isset($image_element['src']) ? $image_element['src'] : '', '#maxlength' => 2048, '#required' => TRUE); // If the editor has image uploads enabled, show a managed_file form item, // otherwise show a (file URL) text form item. if ($image_upload['status']) { $form['attributes']['src']['#access'] = FALSE; $form['attributes']['src']['#required'] = FALSE; } else { $form['fid']['#access'] = FALSE; $form['fid']['#required'] = FALSE; } // The alt attribute is *required*, but we allow users to opt-in to empty // alt attributes for the very rare edge cases where that is valid by // specifying two double quotes as the alternative text in the dialog. // However, that *is* stored as an empty alt attribute, so if we're editing // an existing image (which means the src attribute is set) and its alt // attribute is empty, then we show that as two double quotes in the dialog. // @see https://www.drupal.org/node/2307647 $alt = isset($image_element['alt']) ? $image_element['alt'] : ''; if ($alt === '' && !empty($image_element['src'])) { $alt = '""'; } $form['attributes']['alt'] = array('#title' => $this->t('Alternative text'), '#placeholder' => $this->t('Short description for the visually impaired'), '#type' => 'textfield', '#required' => TRUE, '#required_error' => $this->t('Alternative text is required.<br />(Only in rare cases should this be left empty. To create empty alternative text, enter <code>""</code> — two double quotes without any content).'), '#default_value' => $alt, '#maxlength' => 2048); // When Drupal core's filter_align is being used, the text editor may // offer the ability to change the alignment. if (isset($image_element['data-align']) && $editor->getFilterFormat()->filters('filter_align')->status) { $form['align'] = array('#title' => $this->t('Align'), '#type' => 'radios', '#options' => array('none' => $this->t('None'), 'left' => $this->t('Left'), 'center' => $this->t('Center'), 'right' => $this->t('Right')), '#default_value' => $image_element['data-align'] === '' ? 'none' : $image_element['data-align'], '#wrapper_attributes' => array('class' => array('container-inline')), '#attributes' => array('class' => array('container-inline')), '#parents' => array('attributes', 'data-align')); } // When Drupal core's filter_caption is being used, the text editor may // offer the ability to in-place edit the image's caption: show a toggle. if (isset($image_element['hasCaption']) && $editor->getFilterFormat()->filters('filter_caption')->status) { $form['caption'] = array('#title' => $this->t('Caption'), '#type' => 'checkbox', '#default_value' => $image_element['hasCaption'] === 'true', '#parents' => array('attributes', 'hasCaption')); } $form['actions'] = array('#type' => 'actions'); $form['actions']['save_modal'] = array('#type' => 'submit', '#value' => $this->t('Save'), '#submit' => array(), '#ajax' => array('callback' => '::submitForm', 'event' => 'click')); return $form; }
/** * Form submit handler for mollom_test_form(). */ public function submitForm(array &$form, FormStateInterface $form_state) { // Conditionally enable form caching. if (\Drupal::state()->get('mollom_test.cache_form', FALSE)) { $form_state->setCached(TRUE); } $new_field = $form_state->getValue(array('field', 'new'), ''); if (!empty($new_field)) { $field = $form_state->getValue('field'); $field[] = $new_field; $form_state->setValue('field', $field); } parent::submitForm($form, $form_state); drupal_set_message('Successful form submission.'); }