我创建了一个简单的测试模块,如果以模块配置的形式选中复选框,该模块将向所有节点添加文本横幅。目前,它正在半途而废,如我所愿。
选中复选框并导出配置时,所有节点上都会显示文本横幅。
到目前为止,这是正确的。但是我不想导出要显示的文本横幅的配置。我希望它在选中复选框后立即显示。
一个例子正在发展中。如果您转到Devel的管理区域并选中“Display $page数组”框,页面数组就会显示在每个节点上,而无需导出配置。
若要在使用hook_node_view的所有节点上显示文本横幅,请执行以下操作。然后从我的自定义模块settings.yml文件中获取复选框值。
function my_form_configy_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
$config = \Drupal::config('my_form_configy.settings')->get('checkbox');
if ($config === TRUE) {
$build['layout_test'] = [
'#markup' => ' Extra, Extra read all about it.',
];
}
}这是我的表格
namespace Drupal\my_form_configy\Form;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfigFormBase;
class MyForm extends ConfigFormBase {
public function getFormId() {
return 'my_form_configy_1346';
}
const SETTINGS = 'my_form_configy.settings';
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config(static::SETTINGS);
$check = \Drupal::config('my_form_configy.settings')->get('checkbox');
$form['checkbox'] = [
'#type' => 'checkbox',
'#title' => $this->t('Do you want to add the text banner to all nodes? Checking this box will add a large h2 text banner to add nodes. You must export the config and reload a node page'),
'#default_value' => $check,
'#weight' => '0',
];
$form['#theme'] = 'my_form';
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configFactory->getEditable(static::SETTINGS)
->set('checkbox', $form_state->getValue('checkbox'))
->save();
parent::submitForm($form, $form_state);
}
}Devel使用的方法与我所使用的方法相同,但是如果您选中“Display $page数组”框,dpm特性将立即在节点上工作,而不必导出配置。
function devel_page_attachments_alter(&$page) {
if (\Drupal::currentUser()->hasPermission('access devel information') && \Drupal::config('devel.settings')->get('page_alter')) {
dpm($page, 'page');
}
}我在核心模块和控制模块中搜索了ConfigFormBase,并阅读了下面的内容。https://www.drupal.org/docs/drupal-apis/configuration-api/working-with-configuration-forms和https://www.drupal.org/docs/8/api/form-api/configformbase-with-simple-configuration-api
更新。4k4下面的评论把我放在了正确的位置。根据这篇文章,https://drupal.stackexchange.com/a/266393/58880。我需要应用addCacheableDependency
\Drupal::service('renderer')->addCacheableDependency($variables, $config);因为我使用的是hook_node_view。我把$variables改成了$build
\Drupal::service('renderer')->addCacheableDependency($build, $config);现在,当我单击复选框时,复选框的值是正确的,并且它不会来自配置。文本横幅显示在所有节点上。我的零钱在下面。
function my_form_configy_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
$config = \Drupal::config('my_form_configy.settings')->get('checkbox');
// add the cache tag, so that the output gets invalidated when the config is saved
\Drupal::service('renderer')->addCacheableDependency($build, $config);我想弄明白这是怎么回事?现在存储的“复选框”的值在哪里。它不在我的模块settings.yml文件中,因为我还没有导出配置。它是存储在浏览器缓存中还是Drupal缓存中?因为我正在开发,所以我在settings.local.php中禁用了所有的小枝和drupal缓存。
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';开发模块不使用addCacheableDependency。
单击复选框“Display $page数组”,dpm功能立即工作。
function devel_page_attachments_alter(&$page) {
if (\Drupal::currentUser()->hasPermission('access devel information') && \Drupal::config('devel.settings')->get('page_alter')) {
dpm($page, 'page');
}
}发布于 2020-09-08 18:45:36
hook_ENTITY_TYPE_view()可能是Drupal 8中最被误解/误用的钩子,主要是因为它的行为与Drupal 7中的行为不同,因此您将发现许多在当前版本的Drupal中不再起作用的误导性示例。
简而言之,只有在实体被呈现时才调用hook_ENTITY_TYPE_view() --在实体呈现之后,核心Drupal缓存呈现的实体,并将使用呈现实体的缓存版本。
因此,根据设计,在从缓存加载呈现的实体时,不会再次调用hook_ENTITY_TYPE_view()。具体来说,它并不是每次访问该节点页时都调用的。
有各种各样的其他方式来做你想做的事。由于您已经在使用自己的主题函数,因此可能最容易将配置变量传递给Twig模板,并根据该变量在模板中显示/不显示某些内容。另一种方法是在表单提交时使缓存失效(如果配置值已经更改),以便在下次加载节点(S)时重新呈现。
https://drupal.stackexchange.com/questions/296569
复制相似问题