-1

enter image description here

I Fetched the path field from view using views_pre_render(\Drupal\views\ViewExecutable $view) now I made some changes on that path field but I want path field use my data instead of drupal default path value how can I do that.

 function view_path_views_pre_render(\Drupal\views\ViewExecutable $view) {
  if($view->id() == 'view_idea_shots_for_home_page' && $view->current_display == 
   'block_3') {
    foreach($view->result as $value){
      $path = $view->field['path']->render($value);
    }
     $path = $path['#markup'];
     $path = get_tiny_url($path);
     kint($path);
  }
}

function get_tiny_url($url)  {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

simply I want to alter path field data.

1 Answer 1

0

You need to extend the class Drupal\node\Plugin\views\field\Path, add an annotation with a new plugin name @ViewsField("my_node_path") and override render() to place the new code.

Then add or change the field plugin name in views data:

$data['node']['path'] = [
  'field' => [
    'title' => $this->t('Path'),
    'help' => $this->t('The aliased path to this content.'),
    'id' => 'node_path',    // put here 'my_node_path'
  ],
];

See hook_views_data_alter().

2
  • so I need to write my logic under render() method rihgt??
    – Rudra
    Commented Dec 30, 2017 at 19:36
  • Yes, I suggested to extend the class, but I think this is not necessary, because there is not much code besides that in render() you want to replace. So you can copy it to your own module as well, adjust the plugin name and class namespace and rewrite the code in render().
    – 4uk4
    Commented Dec 31, 2017 at 9:45

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.