DRUPAL8模块开发 - Drupal8渲染layouts

翻译者:长风Drupal开发: Drupal8 layout api

原文链接:https://www.drupal.org/docs/8/api/layout-api/how-to-render-layouts

我们在上一章节中学习了如何使用Drupal8 layout api注册layouts,这一章节,我们学习Drupal8渲染layouts。

获取layouts 插件管理器

$layoutPluginManager = \Drupal::service('plugin.manager.core.layout');

列出可用的layouts

l列出可用的layouts是Drupal8渲染layouts的必要操作。

$layoutPluginManager = \Drupal::service('plugin.manager.core.layout');
$layoutDefinitions = $layoutPluginManager->getDefinitions();
$definedLayouts = [];
foreach ($layoutDefinitions as $key => $layoutDefinition) {
$definedLayouts[] = $layoutDefinition->getLabel();
}
return [
'#theme' => 'item_list',
'#items' => $definedLayouts,
];

例举一个layouts插件

这里有一个Drupal8渲染layouts的例子。

$layoutPluginManager = \Drupal::service('plugin.manager.core.layout');
// Provide any configuration to the layout plugin if necessary.
$layoutInstanceConfiguration = [];
$layoutInstance = $layoutPluginManager->createInstance('layout_twocol', $layoutInstanceConfiguration);
渲染layout
$layoutPluginManager = \Drupal::service('plugin.manager.core.layout');
// Provide any configuration to the layout plugin if necessary.
$layoutInstanceConfiguration = [];
$layoutInstance = $layoutPluginManager->createInstance('layout_twocol', $layoutInstanceConfiguration);

// Build the content for your regions.
$regions = [
'top' => [
'#markup' => 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.',
],
'left' => [
'#markup' => 'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est.',
],
'right' => [
'#markup' => 'At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est.',
],
'bottom' => [
'#markup' => 'At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
],
];

// This builds the render array.
return $layoutInstance->build($regions);

为layout显示配置表单

在Drupal8渲染layouts的实际应用中,为了能够更加灵活,我们可以为layout显示配置表单。

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Plugin\PluginFormInterface;

protected function getLayout($layout_id, $layout_settings, FormStateInterface $form_state) {
if (!$layout_plugin = $form_state->get('layout_plugin')) {
$layout_plugin = $this->layoutPluginManager->createInstance($layout_id, $layout_settings);
$form_state->set('layout_plugin', $layout_plugin);
}
return $layout_plugin;
}

public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
  
// Retrieval of the layout ID and settings is dependent on how it is stored.
$layout_plugin = $this->getLayout($layout_id, $layout_settings, $form_state);
$form['layout'] = [
'#type' => 'select',
'#title' => $this->t('Select a layout'),
'#options' => $this->layoutPluginManager->getLayoutOptions(),
'#default_value' => $layout_plugin->getPluginId(),
];
if ($layout_plugin instanceof PluginFormInterface) {
$form['layout_settings'] = [];
$subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
$form['layout_settings'] = $layout_plugin->buildConfigurationForm($form['layout_settings'], $subform_state);
}
}

public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);

$layout_plugin = $this->getLayout($layout_id, $layout_settings, $form_state);
if ($layout_plugin instanceof PluginFormInterface) {
$subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
$layout_plugin->validateConfigurationForm($form['layout_settings'], $subform_state);
}
}

public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);

$layout_plugin = $this->getLayout($layout_id, $layout_settings, $form_state);
if ($layout_plugin instanceof PluginFormInterface) {
$subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
$layout_plugin->submitConfigurationForm($form['layout_settings'], $subform_state);
}

// @todo This is where you store the updated layout information
$layout_id = $layout_plugin->getPluginId();
$layout_settings = $layout_plugin->getConfiguration();
}