I have create a content type "machines" and I am looking to theme it. At the bottom of each "machine" I would like to create 2 buttons, preview and next. These 2 fields are entity reference of other nodes of the same content type machine.

So I have to create a loop to verify if the field is not empty.

        $next = XXXXXXXXXXX
	$prev = XXXXXXXXXXX

	if ($next != NULL) { 
	  $next_url = XXXXXXXXXXX
        else
	  $next_url = XXXXXXXXXXX
	}

	if ($prev != NULL) { 
	  $prev_url = XXXXXXXXXXX
        else
	  $prev_url = XXXXXXXXXXX
	}

Then I have to print the right field in my buttons.

				<?php if($page && module_exists('prev_next')): ?>	
					<div class="next_prev text-center">
						<ul class="pager">
							<?php if(isset($prev_url)) : ?>
								<li class="previous">
									<a href="<?php echo $prev_url; ?>">← Prev</a>
								</li>
							<?php endif; ?>
							<?php if(isset($next_url)) : ?>
								<li class="next">
									<a href="<?php echo $content->field_next; ?>">Next →</a>
								</li>
							<?php endif; ?>
						</ul>
					</div>
				<?php endif; ?>	

Any idea to replace the XXXXXXXXXXXX? Thank you beforehand!!!!

Comments

Jeff Burnz’s picture

One assumes the module you are using (prev_next) provides those variables, perhaps they are in $links or not, you'd have to view the node object arrays to find where they are.

BTW, there is no loop, if/else are control structures, loops are for, foreach, while etc.

willbxl’s picture

$links does not work :(.
I wrote $node->field_next['target_id']; should work but it does not. It display the url of the node instead of the link to the entity ref.

array(1) {
  ["und"]=>
  array(1) {
    [0]=>
    array(3) {
      ["target_id"]=>
      string(3) "311"
willbxl’s picture

OK Found!

This code does the job

if ($next != NULL) { 
	  $next_url = url('node/' . $node->field_next['und'][0]['target_id'], array('absolute' => TRUE));
	}

Thank you for your help!