mercredi 1 août 2018

In Drupal, if a field is empty, then use another field as the return. proper syntax?

I have come across a problem that I hope is an easy fix but I am just not seeing it. I have wrote some code that targets a certain content type's field.. in that field if it is empty then I want the code to return the value of another field in that content type. I have reworked this several times but I am stuck. I am hoping someone can point out what I am doing wrong. Thanks

Here is the code:

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;  
     }  
    }  
   }  

thanks for any help that can be tossed my way.

Aucun commentaire:

Enregistrer un commentaire