how to alter view path field plugin

by Rudra   Last Updated December 29, 2017 18:07 PM

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.

Tags : views fields


Answers 1


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().

4k4
4k4
December 29, 2017 17:35 PM

Related Questions



Editable fields in views

Updated January 22, 2018 10:07 AM