/** * {@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())))); }
/** * Helper function to independently submit the visibility UI. * * @param array $form * A nested array form elements comprising the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ protected function submitVisibility(array $form, FormStateInterface $form_state) { foreach ($form_state->getValue('visibility') as $condition_id => $values) { // Allow the condition to submit the form. $condition = $form_state->get(['conditions', $condition_id]); $condition->submitConfigurationForm($form['visibility'][$condition_id], SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state)); // Setting conditions' context mappings is the plugins' responsibility. // This code exists for backwards compatibility, because // \Drupal\Core\Condition\ConditionPluginBase::submitConfigurationForm() // did not set its own mappings until Drupal 8.2 // @todo Remove the code that sets context mappings in Drupal 9.0.0. if ($condition instanceof ContextAwarePluginInterface) { $context_mapping = isset($values['context_mapping']) ? $values['context_mapping'] : []; $condition->setContextMapping($context_mapping); } $condition_configuration = $condition->getConfiguration(); // Update the visibility conditions on the block. $this->entity->getVisibilityConditions()->addInstanceId($condition_id, $condition_configuration); } }
/** * 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; }
/** * Private helper method to set the test block's cache configuration. */ private function setBlockCacheConfig($cache_config) { $block = $this->block->getPlugin(); $block->setConfigurationValue('cache', $cache_config); $this->block->save(); }
/** * Generates a unique machine name for a block. * * @param \Drupal\block\BlockInterface $block * The block entity. * * @return string * Returns the unique name. */ public function getUniqueMachineName(BlockInterface $block) { $suggestion = $block->getPlugin()->getMachineNameSuggestion(); // Get all the blocks which starts with the suggested machine name. $query = $this->storage->getQuery(); $query->condition('id', $suggestion, 'CONTAINS'); $block_ids = $query->execute(); $block_ids = array_map(function ($block_id) { $parts = explode('.', $block_id); return end($parts); }, $block_ids); // Iterate through potential IDs until we get a new one. E.g. // 'plugin', 'plugin_2', 'plugin_3', etc. $count = 1; $machine_default = $suggestion; while (in_array($machine_default, $block_ids)) { $machine_default = $suggestion . '_' . ++$count; } return $machine_default; }
/** * Protected helper method to set the test block's configuration. */ protected function setBlockConfiguration($key, $value) { $block = $this->block->getPlugin(); $block->setConfigurationValue($key, $value); $this->block->save(); }
/** * Provides a title callback to get the block's admin label. * * @param \Drupal\block\BlockInterface $block * The block entity. * * @return \Drupal\Core\StringTranslation\TranslatableMarkup * The title. */ public function title(BlockInterface $block) { // @todo Wrap "Configure " in <span class="visually-hidden"></span> once // https://www.drupal.org/node/2359901 is fixed. return $this->t('Configure @block', ['@block' => $block->getPlugin()->getPluginDefinition()['admin_label']]); }