/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); $form_state->setValue('weight', (int) $form_state->getValue('weight')); // The Block Entity form puts all block plugin form elements in the // settings form element, so just pass that to the block for validation. $this->getPluginForm($this->entity->getPlugin())->validateConfigurationForm($form['settings'], SubformState::createForSubform($form['settings'], $form, $form_state)); $this->validateVisibility($form, $form_state); }
/** * {@inheritdoc} */ public function validate(array $form, array &$form_state) { parent::validate($form, $form_state); // The Block Entity form puts all block plugin form elements in the // settings form element, so just pass that to the block for validation. $settings = array('values' => &$form_state['values']['settings']); // Call the plugin validate handler. $this->entity->getPlugin()->validateConfigurationForm($form, $settings); }
/** * {@inheritdoc} */ public function validate(array $form, FormStateInterface $form_state) { parent::validate($form, $form_state); // The Block Entity form puts all block plugin form elements in the // settings form element, so just pass that to the block for validation. $settings = (new FormState())->setValues($form_state->getValue('settings')); // Call the plugin validate handler. $this->entity->getPlugin()->validateConfigurationForm($form, $settings); // Update the original form values. $form_state->setValue('settings', $settings->getValues()); }
/** * 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']]); }