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

function get_tiny_url($url)  {
  $ch = curl_init();
  $timeout = 5;
  $data = curl_exec($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().

December 29, 2017 17:35 PM

Related Questions

Taxonomy exported via REST export - "parent" key is empty

Updated September 21, 2017 12:07 PM