简体   繁体   English

Drupal HOOK_views_pre_render对于$ views-> result的简单更改不起作用

[英]Drupal HOOK_views_pre_render not working for simple change of $views->result

well I haven't been able to find the problem with my little custom module for a couple of days, I'm hoping some clever and kind person might spot my issue ;) 好几天来,我的小自定义模块都找不到问题了,我希望有个聪明,友善的人可以发现我的问题;)

So I am creating a simple module that runs on a certain view type, this view contains images, the images have alt tags, I want to amend these alt tags by using the url alias and sticking this on the end of the alt tag.. 因此,我正在创建一个在特定视图类型上运行的简单模块,该视图包含图像,图像具有alt标签,我想使用url别名修改这些alt标签,并将其粘贴在alt标签的末尾。

I have successfully found the data in the $view array and I have looped through all image instances and done as said above, I know it has worked because I have printed them out onto the page. 我已经成功地在$ view数组中找到了数据,并且如上所述遍历了所有图像实例,并知道这样做是有效的,因为我已经将它们打印到了页面上。

However it is not updating the view on the actual page, the alt tags remain the same... Please excuse my ignorance here as I am learning PHP and multidimensional keyed arrays are taking some time to sink in. 但是,它并没有更新实际页面上的视图,alt标签保持不变...请原谅我的无知,因为我正在学习PHP,而多维键控数组也需要花费一些时间。

So what is the variable called for the alt tag, an object? 那么,alt标签(对象)的变量是什么? I think that's wrong but anyway.. This variable which is .. 我认为这是错误的,但是无论如何..这个变量是..

$view->result[0]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt']

... is being changed, everything seems fine inside the function and I have tried ...正在被更改,该函数内部的一切似乎都很好,我已经尝试过

return $view

but to no avail. 但无济于事。

Here is my code in entirety 这是我的全部代码

<?php
  function image_alt_tag_alter_views_pre_render(&$view) {

    if ($view->name == "we_print_for_menu") {
    $path = arg(0) . "/" . arg(1);
    $alias = drupal_get_path_alias($path);


      // loops through index in array to change each alt tag

      for ($i = 0; $i < count($view->result); ++$i) {
            $view->result[$i]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt'] = $view->result[$i]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt'] . $alias;
          }

      return $view;
?>
<?php

function image_alt_tag_alter_views_post_execute(&$view) {
  global $user;
  if ($view->name == "we_print_for_menu") {
    path = arg(0) . "/" . arg(1);
    $alias = drupal_get_path_alias($path);

    dsm($view);

    for ($i = 0; $i < count($view->result); ++$i) {
       $amended_alt = $view->result[$i]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt'] . " " . $alias;
       $view->result[$i]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt'] = $amended_alt;
    }
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM