Php 在Drupal中,如果一个字段为空,则使用另一个字段作为返回。正确的语法?
我遇到了一个问题,我希望是一个容易解决,但我只是没有看到它。我已经编写了一些针对特定内容类型字段的代码。。在该字段中,如果为空,则我希望代码返回该内容类型中另一个字段的值。我已经修改了好几次了,但是我被卡住了。我希望有人能指出我做错了什么。谢谢 代码如下:Php 在Drupal中,如果一个字段为空,则使用另一个字段作为返回。正确的语法?,php,if-statement,drupal-8,isnullorempty,Php,If Statement,Drupal 8,Isnullorempty,我遇到了一个问题,我希望是一个容易解决,但我只是没有看到它。我已经编写了一些针对特定内容类型字段的代码。。在该字段中,如果为空,则我希望代码返回该内容类型中另一个字段的值。我已经修改了好几次了,但是我被卡住了。我希望有人能指出我做错了什么。谢谢 代码如下: function randomname_preprocess_node__events__sfa_teaser(&$variables) { // Populates the title variable from the n
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;
}
}
}
感谢您提供的帮助。我根本看不到任何返回值,但是&$variables正在通过引用传递,这意味着函数正在修改调用范围中的$variables。也许如果你这样做会有所帮助——测试你要测试的空值,然后相应地设置$variables。谢谢Bill,我还是有点迷茫,但我正在努力找出答案。我对这种类型的编码还是新手。所以我花了一段时间才弄明白。如果我有任何进展,我会更新这个。再次感谢您的评论