0

I have a content type names Link in which contains only a link field. I want to rewrite the title link to the external link(link field's value),but I couldn't get link field's value under hook_views_pre_render, although I could get title,body,created... value without any problem ,I just couldn't get the value of link field which I added to the content type. core: Drupal 10

Blockquote Drupal\views\ResultRow {#11171 ▼ +_entity: Drupal\node\Entity
Node {#11292 ▼ #entityTypeId: "node" #enforceIsNew: null #typedData: null #cacheContexts: [] #cacheTags: [] #cacheMaxAge: -1 #_serviceIds: [] #_entityStorages: [] #values: array:21 [▼ "nid" => array:1 [▶] "vid" => array:1 [▶] "type" => array:1 [▶] "uuid" => array:1 [▶] "langcode" => array:1 [▶] "revision_uid" => array:1 [▶] "revision_timestamp" => array:1 [▶] "revision_log" => array:1 [▶] "revision_default" => array:1 [▶] "isDefaultRevision" => array:1 [▶] "status" => array:1 [▶] "uid" => array:1 [▶] "title" => array:1 [▶] "created" => array:1 [▶] "changed" => array:1 [▶] "promote" => array:1 [▶] "sticky" => array:1 [▶] "default_langcode" => array:1 [▶] "revision_translation_affected" => array:1 [▶] "field_comment2" => array:1 [▶] "field_link" => array:1 [▶] ] #fields: [] #fieldDefinitions: null #languages: null #langcodeKey: "langcode" #defaultLangcodeKey: "default_langcode" #activeLangcode: "x-default" #enforceDefaultTranslation: null #defaultLangcode: "en" #translations: array:1 [▶] #translationInitialize: false #newRevision: false #isDefaultRevision: "1" #entityKeys: array:4 [▶] #translatableEntityKeys: array:8 [▶] #validated: false #validationRequired: false #loadedRevisionId: "214" #revisionTranslationAffectedKey: "revision_translation_affected" #enforceRevisionTranslationAffected: [] #isSyncing: false +in_preview: null } +_relationship_entities: array:1 [▶] +index: 0 +"node_field_data_created": "1718470654" +"nid": "214" +"users_field_data_node_field_data_uid": "36" } ... Blockquote

function showcontent_views_pre_render(ViewExecutable $view){


    // Check if we are working with the correct view and display.
    if ($view->id() == 'newest' ) {
        // Iterate through the rendered result to find the title field and rewrite it.

        foreach ($view->result as $key => $row) {
    

            if($row->_entity->hasField('field_link')){

                $current_title = $row->_entity->get('title')->value;
                dpm($current_title); //get node title
                $external_link_uri = $row->_entity->get('field_link')->value;
                dpm($external_link_uri); //get null value



            }else{

            }


        }

    }
}

2 Answers 2

2

You can write it much shorter:

$node = $row->_entity;
if ($node->getType() == 'link') {
  $title = $node->title->value;
  $uri = $node->field_link->uri;

For the link field type the database column containing the field data is uri.

3
  • In fact,I tested "$row->field_link->uri;" before I posted this question, it wouldn't work and returned null for uri value. Thanks!
    – user167043
    Commented Jun 17 at 8:07
  • This code is missing _entity. You better do it step by step. First get $node, then check if it is the content type and then you can be sure you find the fields added to this content type.
    – 4uk4
    Commented Jun 17 at 8:56
  • I did has _entity on the script but it wouldn't work for link field , returned result was null.
    – user167043
    Commented Jun 19 at 1:34
1

Well, I solved it by myself just now , I am not sure if this is the best way though, if you have a better way, it would be appreciated you can post it here.

my way:~

function showcontent_views_pre_render(ViewExecutable $view){


// Check if we are working with the correct view and display.
if ($view->id() == 'newest' ) {
    // Iterate through the rendered result to find the title field and rewrite it.

    foreach ($view->result as $key => $row) {
        dpm($row);

        if($row->_entity->hasField('field_link')){

            $current_title = $row->_entity->get('title')->value;
            dpm($current_title); //get node title
            $external_link_uri = $row->_entity->get('field_link')->first()->getValue()['uri'];   // this is the trick
            dpm($external_link_uri); //get uri value
  


        }else{

        }

    }

}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.