0

The context: drupal 7, views, views PHP, rendering some fields with PHP because there's no other way.

$nodo = node_load($row->nid);
$strmn  = '<div class="post-block">';
$strmn .= '<div class="post-image">';
$image = field_get_items('node', $nodo, 'field_image');
$preout= field_view_value('node', $nodo, 'field_image', $image[0]);
$linko= url('node/'. $nodo->nid);
$strmn .= '<a href="' . $linko . '">' . render($preout) . '</a>';

After a lot of googling I've been able to write this.

It's very probably crude but it works, except I can't find a proper way to set height and width for the image. I don't want to create a style in drupal, it would be no good as it won't be used anywhere else. I also don't want, if at all possible, to rework my code to do something like:

$image = theme('image_style',array(
    'style_name' => 'mail_image_big',
    'path' => $image_obj['uri'],
    'attributes' => array('style' => 'border:1px solid #aaa;')
  )
);

Because my php is sketchy at best and the knowledge of the drupal API is null. Every change costs me a lot of time and energy because, honestly, I lack the basics and I'm not sure of what I'm doing until I do it right.

Finally: how do I set the $display array in field_view_value so that height and width are set the way I want them in my output html? I can't find an example or a list of valid properties I can set in the $settings array to save my life. :)

1

The context: drupal 7, views, views PHP, rendering some fields with PHP because there's no other way.

There definitely is another way, you're just not aware of it, this article was written 5 years ago:

As of this writing, 44,497 site maintainers have read that warning and chosen to ignore it. They’ve chosen to put their PHP into a non-revisioned, difficult-to-access place, and to enable PHP input in a module that was never designed for security. They’ve left their site at risk of a very difficult to diagnose and even harder to fix WSOD.

I suggest that you fully read and understand that article and rethink your position about there being no other way.

Because my PHP is sketchy at best and the knowledge of the drupal API is null. Every change costs me a lot of time and energy because, honestly, I lack the basics and I'm not sure of what I'm doing until I do it right.

Welcome to Drupal, it's a steep learning curve and if your knowledge of PHP is poor then you stand no chance of being able to understand Drupal, perhaps you could edit your question or provide a comment of what you're trying to achieve? I'm always surprised that beginner Drupalers dive straight into coding their way round a problem before investigating whether there is a non-code solution.

Finally, who cares whether an image style is going to be used anywhere else or not? It's not they are hard to set-up or cost anything.

  • Thanks for your reply. You touch a number of good points, both on purpose and by chance, more on that later maybe. What I truly need is that a view that renders pages belonging to a taxonomy changes its output based on the taxonomy term. At first I tried to do it modifying the view but it breaks as soon as I add a condition based on the taxonomy term. Googling around I found a comment that views just "don't work that way". If you know better, by all means point me in the right direction. The same goes for changes to the above php code, it's not like I'm against them but I do need guidance. – meh Sep 24 '18 at 7:56
  • From what you describe it appears you want to add a Contextual filter to your View (it's underneath Advanced). I think you will need to use Content: Has taxonomy term ID and use the preview section at the bottom to test – Colin Shipton Sep 24 '18 at 8:20
  • The view is indeed setup that way. I tried to create a display that would activate for a certain specific taxonomy term, while the default one would have handled all the others, but it breaks, as I said before. On your advice I double checked: the display with filter criteria taxonomy term <> x is shown in the appropriate pages, the one with taxonomy term = x however gives blank output. I inspected the html, the container div is empty, not a line in it. – meh Sep 24 '18 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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