Afternoon,
I'm using the Views module, and I've created a custom display-mode called 'Image Only'. I'm trying to write a module that will either (A) display a summary of the body or (B) change the view mode to 'teaser' if the node does not have an image.

On the clients old site, this was done in one line of PHP, so you can imagine my confusion hahaha.

I have tried using

function displaymodes_node_view_alter(&$build) {
    $build['#view_mode'] = 'teaser';
}

This will change the HTML to display a teaser (it adds the node-teaser class to the node), but since this is post-build, I'm guessing it doesn't pull the values.

Then, I tried

function displaymodes_views_pre_build(&$view) {
    $view->display['default']->display_options['row_options']['view_mode'] = 'teaser';
}

This does very little;
It doesn't help that each variable has between 10 and 70 instances of 'view_mode' buried in the object/array

Has anyone tried doing this before?

Comments

ntomsheck’s picture

Alright, figured out a solution. I had to attack the problem backwards though; Instead of telling it to never display the summary, but then try to call it when their was no image, I decided to always display the summary, but hide it when there IS an image:

function displaymodes_node_view_alter(&$build) {
	if($build['#view_mode'] == 'image_only') {
		if($build['body']['#object']->field_image) {
			$build['body'][0]['#markup'] = "";
		}
	} 	 
}

Dirty? Maybe. Functional? Absolutely.