/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); $entity = $this->entity; // The Block Entity form puts all block plugin form elements in the // settings form element, so just pass that to the block for submission. // @todo Find a way to avoid this manipulation. $settings = (new FormState())->setValues($form_state->getValue('settings')); // Call the plugin submit handler. $entity->getPlugin()->submitConfigurationForm($form, $settings); // Update the original form values. $form_state->setValue('settings', $settings->getValues()); // Submit visibility condition settings. foreach ($form_state->getValue('visibility') as $condition_id => $values) { // Allow the condition to submit the form. $condition = $form_state->get(['conditions', $condition_id]); $condition_values = (new FormState())->setValues($values); $condition->submitConfigurationForm($form, $condition_values); if ($condition instanceof ContextAwarePluginInterface) { $context_mapping = isset($values['context_mapping']) ? $values['context_mapping'] : []; $condition->setContextMapping($context_mapping); } // Update the original form values. $condition_configuration = $condition->getConfiguration(); $form_state->setValue(['visibility', $condition_id], $condition_configuration); // Update the visibility conditions on the block. $entity->getVisibilityConditions()->addInstanceId($condition_id, $condition_configuration); } // Save the settings of the plugin. $entity->save(); drupal_set_message($this->t('The block configuration has been saved.')); $form_state->setRedirect('block.admin_display_theme', array('theme' => $form_state->getValue('theme')), array('query' => array('block-placement' => Html::getClass($this->entity->id())))); }
/** * {@inheritdoc} */ public function submit(array $form, array &$form_state) { parent::submit($form, $form_state); $entity = $this->entity; // The Block Entity form puts all block plugin form elements in the // settings form element, so just pass that to the block for submission. // @todo Find a way to avoid this manipulation. $settings = array('values' => &$form_state['values']['settings'], 'errors' => $form_state['errors']); // Call the plugin submit handler. $entity->getPlugin()->submitConfigurationForm($form, $settings); // Save the settings of the plugin. $entity->save(); drupal_set_message($this->t('The block configuration has been saved.')); $form_state['redirect_route'] = array('route_name' => 'block.admin_display_theme', 'route_parameters' => array('theme' => $form_state['values']['theme']), 'options' => array('query' => array('block-placement' => drupal_html_class($this->entity->id())))); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); $entity = $this->entity; // The Block Entity form puts all block plugin form elements in the // settings form element, so just pass that to the block for submission. // @todo Find a way to avoid this manipulation. $settings = (new FormState())->setValues($form_state->getValue('settings')); // Call the plugin submit handler. $entity->getPlugin()->submitConfigurationForm($form, $settings); // Update the original form values. $form_state->setValue('settings', $settings->getValues()); // Save the settings of the plugin. $entity->save(); drupal_set_message($this->t('The block configuration has been saved.')); $form_state->setRedirect('block.admin_display_theme', array('theme' => $form_state->getValue('theme')), array('query' => array('block-placement' => drupal_html_class($this->entity->id())))); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); $entity = $this->entity; // The Block Entity form puts all block plugin form elements in the // settings form element, so just pass that to the block for submission. $sub_form_state = SubformState::createForSubform($form['settings'], $form, $form_state); // Call the plugin submit handler. $block = $entity->getPlugin(); $this->getPluginForm($block)->submitConfigurationForm($form, $sub_form_state); // If this block is context-aware, set the context mapping. if ($block instanceof ContextAwarePluginInterface && $block->getContextDefinitions()) { $context_mapping = $sub_form_state->getValue('context_mapping', []); $block->setContextMapping($context_mapping); } $this->submitVisibility($form, $form_state); // Save the settings of the plugin. $entity->save(); drupal_set_message($this->t('The block configuration has been saved.')); $form_state->setRedirect('block.admin_display_theme', array('theme' => $form_state->getValue('theme')), array('query' => array('block-placement' => Html::getClass($this->entity->id())))); }
/** * Builds a #pre_render-able block render array. * * @param \Drupal\block\BlockInterface $entity * A block config entity. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler service. * * @return array * A render array with a #pre_render callback to render the block. */ protected static function buildPreRenderableBlock($entity, ModuleHandlerInterface $module_handler) { $plugin = $entity->getPlugin(); $plugin_id = $plugin->getPluginId(); $base_id = $plugin->getBaseId(); $derivative_id = $plugin->getDerivativeId(); $configuration = $plugin->getConfiguration(); // Inject runtime contexts. if ($plugin instanceof ContextAwarePluginInterface) { $contexts = \Drupal::service('context.repository')->getRuntimeContexts($plugin->getContextMapping()); \Drupal::service('context.handler')->applyContextMapping($plugin, $contexts); } // Create the render array for the block as a whole. // @see template_preprocess_block(). $build = ['#theme' => 'block', '#attributes' => [], '#contextual_links' => ['block' => ['route_parameters' => ['block' => $entity->id()]]], '#weight' => $entity->getWeight(), '#configuration' => $configuration, '#plugin_id' => $plugin_id, '#base_plugin_id' => $base_id, '#derivative_plugin_id' => $derivative_id, '#id' => $entity->id(), '#pre_render' => [static::class . '::preRender'], '#block' => $entity]; // If an alter hook wants to modify the block contents, it can append // another #pre_render hook. $module_handler->alter(['block_view', "block_view_{$base_id}"], $build, $plugin); return $build; }