我遇到了一个问题,我希望这是一个简单的解决方法,但我只是没有看到。我写了一些针对某个特定内容类型字段的代码。如果该字段为空,那么我希望代码返回该内容类型中另一个字段的值。我已经对此进行了多次重做,但是我被卡住了。我希望有人能指出我做错了什么。谢谢
代码如下:
function randomname_preprocess_node__events__sfa_teaser(&$variables) {
// Populates the title variable from the node revision being viewed.
$route_match = \Drupal::routeMatch();
$node = $route_match->getParameter('node');
if ($node) {
if (is_string($node)) {
$node = Node::load($node);
}
$content_type = $node->getType();
if ($content_type === 'randomcontenttypename') {
$parent_title = $node->title->value;
$generic_link = $node->get('field_generic')->uri;
$populated_link_text = $node->get('field_populated_link_text')->value;
if ($entity->get('populated_link_text')) {
$populated_link_text = $entity->get('populated_link_text')->entity;
$variables['populated_link_text'] = [
'#type' => 'link',
'#title' => $populated_link_text->value,
'#url' => $populated_link_text,
];
}
else {
if ($generic_link) {
$generic_link = Url::fromUri($generic_link);
$variables['generic_link'] = [
'#type' => 'link',
'#url' => $generic_link,
'#title' => t('This page will display @parent_title', ['@parent_title' => $parent_title]),
'#attributes' => ['class' => 'viewalllink'],
];
}
$variables['#cache']['max-age'] = 0;
}
}
}
感谢您能给予我的帮助。