/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $contexts = $form_state->getTemporaryValue('gathered_contexts') ?: []; $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts); $form['negate'] = array('#type' => 'checkbox', '#title' => $this->t('Negate the condition'), '#default_value' => $this->configuration['negate']); return $form; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $cached_values = $form_state->getTemporaryValue('wizard'); /** @var $policy \Drupal\password_policy\Entity\PasswordPolicy */ $policy = $cached_values['password_policy']; $policy->set('roles', array_filter($form_state->getValue('roles'))); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $cached_values = $form_state->getTemporaryValue('wizard'); /** @var $page \Drupal\ctools_wizard_test\Entity\ExampleConfigEntity */ $config_entity = $cached_values['ctools_wizard_test_config_entity']; $config_entity->set('two', $form_state->getValue('two')); }
/** * {@inheritdoc} */ public function finish(array &$form, FormStateInterface $form_state) { $cached_values = $form_state->getTemporaryValue('wizard'); drupal_set_message($this->t('Value One: @one', ['@one' => $cached_values['one']])); drupal_set_message($this->t('Value Two: @two', ['@two' => $cached_values['two']])); parent::finish($form, $form_state); }
/** * Form submission handler. * * @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. */ public function submitForm(array &$form, FormStateInterface $form_state) { $keys = array('two'); $cached_values = $form_state->getTemporaryValue('wizard'); foreach ($keys as $key) { $cached_values[$key] = $form_state->getValue($key); } $form_state->setTemporaryValue('wizard', $cached_values); }
/** * Form submission handler. * * @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. */ public function submitForm(array &$form, FormStateInterface $form_state) { $keys = array('one'); $cached_values = $form_state->getTemporaryValue('wizard'); foreach ($keys as $key) { $cached_values[$key] = $form_state->getValue($key); } $form_state->setTemporaryValue('wizard', $cached_values); drupal_set_message($this->t('Dynamic value submitted: @value', ['@value' => $cached_values['dynamic']])); }
public function add(array &$form, FormStateInterface $form_state) { $condition = $form_state->getValue('conditions'); $content = \Drupal::formBuilder()->getForm($this->getConditionClass(), $condition, $this->getTempstoreId(), $this->machine_name); $content['#attached']['library'][] = 'core/drupal.dialog.ajax'; $cached_values = $form_state->getTemporaryValue('wizard'); list(, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('conditions')); $content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $this->url($this->getAddRoute($cached_values), $route_parameters, ['query' => [FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]]); $response = new AjaxResponse(); $response->addCommand(new OpenModalDialogCommand($this->t('Configure Required Context'), $content, array('width' => '700'))); return $response; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $cached_values = $form_state->getTemporaryValue('wizard'); $this->machine_name = $cached_values['id']; $form['#attached']['library'][] = 'core/drupal.dialog.ajax'; $options = []; foreach ($this->typedDataManager->getDefinitions() as $plugin_id => $definition) { $options[$plugin_id] = (string) $definition['label']; } $form['items'] = array('#type' => 'markup', '#prefix' => '<div id="configured-contexts">', '#suffix' => '</div>', '#theme' => 'table', '#header' => array($this->t('Information'), $this->t('Description'), $this->t('Operations')), '#rows' => $this->renderContexts($cached_values), '#empty' => t('No required contexts have been configured.')); $form['contexts'] = ['#type' => 'select', '#options' => $options]; $form['add'] = ['#type' => 'submit', '#name' => 'add', '#value' => t('Add required context'), '#ajax' => ['callback' => [$this, 'add'], 'event' => 'click'], '#submit' => ['callback' => [$this, 'submitform']]]; return $form; }
/** * {@inheritdoc} */ public function finish(array &$form, FormStateInterface $form_state) { $cached_values = $form_state->getTemporaryValue('wizard'); /** @var $entity \Drupal\Core\Entity\EntityInterface */ $entity = $cached_values[$this->getEntityType()]; $entity->set('id', $cached_values['id']); $entity->set('label', $cached_values['label']); $status = $entity->save(); $definition = $this->entityManager->getDefinition($this->getEntityType()); if ($status) { drupal_set_message($this->t('Saved the %label !entity_type.', array('%label' => $entity->label(), '!entity_type' => $definition->getLabel()))); } else { drupal_set_message($this->t('The %label !entity_type was not saved.', array('%label' => $entity->label(), '!entity_type' => $definition->getLabel()))); } $form_state->setRedirectUrl($entity->urlInfo('collection')); parent::finish($form, $form_state); }
/** * {@inheritdoc} * * Creates a generic configuration form for all block types. Individual * block plugins can add elements to this form by overriding * BlockBase::blockForm(). Most block plugins should not override this * method unless they need to alter the generic form elements. * * @see \Drupal\Core\Block\BlockBase::blockForm() */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $definition = $this->getPluginDefinition(); $form['provider'] = array('#type' => 'value', '#value' => $definition['provider']); $form['admin_label'] = array('#type' => 'item', '#title' => $this->t('Block description'), '#plain_text' => $definition['admin_label']); $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Title'), '#maxlength' => 255, '#default_value' => $this->label(), '#required' => TRUE); $form['label_display'] = array('#type' => 'checkbox', '#title' => $this->t('Display title'), '#default_value' => $this->configuration['label_display'] === BlockInterface::BLOCK_LABEL_VISIBLE, '#return_value' => BlockInterface::BLOCK_LABEL_VISIBLE); // Add context mapping UI form elements. $contexts = $form_state->getTemporaryValue('gathered_contexts') ?: []; $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts); // Add plugin-specific settings for this block type. $form += $this->blockForm($form, $form_state); return $form; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $cached_values = $form_state->getTemporaryValue('wizard'); list($route_name, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('contexts')); $form_state->setRedirect($route_name . '.edit', $route_parameters); }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $embed_button = $form_state->getTemporaryValue('embed_button'); $entity_type_id = $this->getConfigurationValue('entity_type'); $form['entity_type'] = array('#type' => 'select', '#title' => $this->t('Entity type'), '#options' => $this->getEntityTypeOptions(), '#default_value' => $entity_type_id, '#description' => $this->t("Entity type for which this button is to enabled."), '#required' => TRUE, '#ajax' => array('callback' => array($form_state->getFormObject(), 'updateTypeSettings'), 'effect' => 'fade'), '#disabled' => !$embed_button->isNew()); if ($entity_type_id) { $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); $form['bundles'] = array('#type' => 'checkboxes', '#title' => $entity_type->getBundleLabel() ?: $this->t('Bundles'), '#options' => $this->getEntityBundleOptions($entity_type), '#default_value' => $this->getConfigurationValue('bundles'), '#description' => $this->t('If none are selected, all are allowed.')); $form['bundles']['#access'] = !empty($form['bundles']['#options']); // Allow option to limit Entity Embed Display plugins. $form['display_plugins'] = array('#type' => 'checkboxes', '#title' => $this->t('Allowed Entity Embed Display plugins'), '#options' => $this->displayPluginManager->getDefinitionOptionsForEntityType($entity_type_id), '#default_value' => $this->getConfigurationValue('display_plugins'), '#description' => $this->t('If none are selected, all are allowed. Note that these are the plugins which are allowed for this entity type, all of these might not be available for the selected entity.')); $form['display_plugins']['#access'] = !empty($form['display_plugins']['#options']); /** @var \Drupal\entity_browser\EntityBrowserInterface[] $browsers */ if ($this->entityTypeManager->hasDefinition('entity_browser') && ($browsers = $this->entityTypeManager->getStorage('entity_browser')->loadMultiple())) { $ids = array_keys($browsers); $labels = array_map(function ($item) { /** @var \Drupal\entity_browser\EntityBrowserInterface $item */ return $item->label(); }, $browsers); $options = ['_none' => $this->t('None (autocomplete)')] + array_combine($ids, $labels); $form['entity_browser'] = ['#type' => 'select', '#title' => $this->t('Entity browser'), '#description' => $this->t('Entity browser to be used to select entities to be embedded.'), '#options' => $options, '#default_value' => $this->getConfigurationValue('entity_browser')]; $form['entity_browser_settings'] = ['#type' => 'details', '#title' => $this->t('Entity browser settings'), '#open' => TRUE, '#states' => ['invisible' => [':input[name="type_settings[entity_browser]"]' => ['value' => '_none']]]]; $form['entity_browser_settings']['display_review'] = ['#type' => 'checkbox', '#title' => 'Display the entity after selection', '#default_value' => $this->getConfigurationValue('entity_browser_settings')['display_review']]; } else { $form['entity_browser'] = ['#type' => 'value', '#value' => '']; } } return $form; }
/** * {@inheritdoc} */ public function hasTemporaryValue($key) { return $this->mainFormState->getTemporaryValue($key); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; $entity_browser->setName($form_state->getValue('id'))->setLabel($form_state->getValue('label'))->setDisplay($form_state->getValue('display'))->setWidgetSelector($form_state->getValue('widget_selector'))->setSelectionDisplay($form_state->getValue('selection_display')); }
/** * Helper function for building the visibility UI form. * * @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 array with the visibility UI added in. */ protected function buildVisibilityInterface(array $form, FormStateInterface $form_state) { $form['visibility_tabs'] = ['#type' => 'vertical_tabs', '#title' => $this->t('Visibility'), '#parents' => ['visibility_tabs'], '#attached' => ['library' => ['block/drupal.block']]]; // @todo Allow list of conditions to be configured in // https://www.drupal.org/node/2284687. $visibility = $this->entity->getVisibility(); foreach ($this->manager->getDefinitionsForContexts($form_state->getTemporaryValue('gathered_contexts')) as $condition_id => $definition) { // Don't display the current theme condition. if ($condition_id == 'current_theme') { continue; } // Don't display the language condition until we have multiple languages. if ($condition_id == 'language' && !$this->language->isMultilingual()) { continue; } /** @var \Drupal\Core\Condition\ConditionInterface $condition */ $condition = $this->manager->createInstance($condition_id, isset($visibility[$condition_id]) ? $visibility[$condition_id] : []); $form_state->set(['conditions', $condition_id], $condition); $condition_form = $condition->buildConfigurationForm([], $form_state); $condition_form['#type'] = 'details'; $condition_form['#title'] = $condition->getPluginDefinition()['label']; $condition_form['#group'] = 'visibility_tabs'; $form[$condition_id] = $condition_form; } if (isset($form['node_type'])) { $form['node_type']['#title'] = $this->t('Content types'); $form['node_type']['bundles']['#title'] = $this->t('Content types'); $form['node_type']['negate']['#type'] = 'value'; $form['node_type']['negate']['#title_display'] = 'invisible'; $form['node_type']['negate']['#value'] = $form['node_type']['negate']['#default_value']; } if (isset($form['user_role'])) { $form['user_role']['#title'] = $this->t('Roles'); unset($form['user_role']['roles']['#description']); $form['user_role']['negate']['#type'] = 'value'; $form['user_role']['negate']['#value'] = $form['user_role']['negate']['#default_value']; } if (isset($form['request_path'])) { $form['request_path']['#title'] = $this->t('Pages'); $form['request_path']['negate']['#type'] = 'radios'; $form['request_path']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value']; $form['request_path']['negate']['#title_display'] = 'invisible'; $form['request_path']['negate']['#options'] = [$this->t('Show for the listed pages'), $this->t('Hide for the listed pages')]; } if (isset($form['language'])) { $form['language']['negate']['#type'] = 'value'; $form['language']['negate']['#value'] = $form['language']['negate']['#default_value']; } return $form; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; $table = $form_state->getValue('table'); /** @var \Drupal\entity_browser\WidgetInterface $widget */ foreach ($entity_browser->getWidgets() as $uuid => $widget) { $widget->submitConfigurationForm($form, $form_state); $widget->setWeight($table[$uuid]['weight']); $widget->setLabel($table[$uuid]['label']); } }
/** * {@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 submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; $this->getPlugin($entity_browser)->submitConfigurationForm($form, $form_state); }
public function ajaxPrevious(array $form, FormStateInterface $form_state) { $cached_values = $form_state->getTemporaryValue('wizard'); $response = new AjaxResponse(); $parameters = $this->getPreviousParameters($cached_values); $response->addCommand(new OpenModalWizardCommand(get_class($this), $this->getTempstoreId(), $parameters)); return $response; }
/** * {@inheritdoc} */ public function &getTemporaryValue($key) { return $this->decoratedFormState->getTemporaryValue($key); }