vdchristelle
10/25/2013 - 9:42 AM

custom menu block of siblings with icon

custom menu block of siblings with icon

<?php
$max = count(element_children($nodes));
// Set up striping values.
$count = 0;
?>
<div class="overview realisaties homepage">
  <ul>
    <?php foreach ($nodes as $node) {
    		$count++;
    	  	$attributes = array();
          $attributes['class'][] = 'colorcode-'.$node->nid;

    	  	$attributes['class'][] = '' . ($count % 2 ? 'odd' : 'even');

    	  	if ($count % 3 == 0) {
    	      $attributes['class'][] = 'third';
    	    }

          if ($count % 4 == 0) {
            $attributes['class'][] = 'fourth';
          }
    	?>
      <?php
        $path = drupal_get_path_alias('node/'.$node->nid);
        if( isset($node->field_icon['und'][0]['uri']) && !empty($node->field_icon['und'][0]['uri'])){
          $image_style = image_style_url('icon', $node->field_icon['und'][0]['uri']);
          dpm($node->field_icon['und'][0]['uri']);
        } else {
          $uri = 'public://icons/'.'default-icon-512.png';
          $image_style = image_style_url('icon', $uri);
        }
        
      ?>

      <li <?php print drupal_attributes($attributes); ?>>
          <?php print '<img src="'.$image_style.'"/>'; ?>
          <?php print '<a href="'.$path.'">'.$node->title.'</a>'; ?>
          <?php print '<h2>'.$node->title.'</h2>'; ?>
      </li>
    <?php } ?>
  </ul>
</div>
<?php
/**
 * Implementation of hook_block_info().
 */
function the_aim_custom_block_info() {

  	// siblings block
  	$blocks['siblings'] = array(
  		'info' => t('Sibling pages'),
  	);
  	
  	// subpages block
  	$blocks['subpages'] = array(
  		'info' => t('Subpages'),
  	);
 
   	return $blocks;
}

/**
 * Implementation of hook_block_view().
 */
function the_aim_custom_block_view($delta='') {
	$block = array();
	global $language;
  	switch ($delta) {
  		
		case 'siblings':
			$block['subject'] = t('Sibling pages');
      $block['content'] = _siblings();
		break;
		
		case 'subpages':
			$block['subject'] = t('Subpages');
      $block['content'] = _subpages();
		break;

  	}

  	return $block;
}

/*
 * Menu block siblings
 * content function
 */
function _siblings() {
 	global $language;

	$node = menu_get_object();
  
	$parent = db_select('menu_links', 'm')
	->fields('m', array('plid'))
	->condition('link_path', 'node/' . $node->nid)
	->execute()->fetchField();

	if (isset($parent) && $parent != 0) {
		$children = db_select('menu_links', 'm')
		->fields('m', array('link_path'))
		->condition('plid', $parent)
		->execute()->fetchCol();
	}

	if (isset($children) && count($children > 0)) {
		$regex = '/([^\/]*)\/([\d]+)/';
		$matches = array();
		foreach($children as $link_path){
			preg_match($regex, $link_path, $matches);
			if(isset($matches[2])){
				$node_results[] = $matches[2];
			}
		}
	}

	if ($node_results) {
		    $nids = array();

		    foreach ($node_results as $node_result) {
		      $nids[] = $node_result;
		    }

		    if (!empty($nids)) {
		      $nodes = node_load_multiple($nids);

		      return array(
		          array(
		              '#theme' => 'subpages',
		              '#nodes' => $nodes,
		          ),
		      );
		      ;
		    }
	  }

  return NULL;
}

/*
 * Menu block children
 * content function
 */
function _subpages() {
 	global $language;

	$node = menu_get_object();
  
	$parent = db_select('menu_links', 'm')
	->fields('m', array('mlid'))
	->condition('link_path', 'node/' . $node->nid)
	->execute()->fetchField();

	if (isset($parent) && $parent != 0) {
		$children = db_select('menu_links', 'm')
		->fields('m', array('link_path'))
		->condition('plid', $parent)
		->execute()->fetchCol();
	}

	if (isset($children) && count($children > 0)) {
		$regex = '/([^\/]*)\/([\d]+)/';
		$matches = array();
		foreach($children as $link_path){
			preg_match($regex, $link_path, $matches);
			if(isset($matches[2])){
				$node_results[] = $matches[2];
			}
		}
	}

	if ($node_results) {
		    $nids = array();

		    foreach ($node_results as $node_result) {
		      $nids[] = $node_result;
		    }

		    if (!empty($nids)) {
		      $nodes = node_load_multiple($nids);

		      return array(
		          array(
		              '#theme' => 'subpages',
		              '#nodes' => $nodes,
		          ),
		      );
		      ;
		    }
	  }

  return NULL;
}

function the_aim_custom_theme() {
  return array(
    'siblings' => array(
      'variables' => array(
          'nodes' => NULL
      ),
      'template' => 'block-subpages'
    ),

    'subpages' => array(
      'variables' => array(
          'nodes' => NULL
      ),
      'template' => 'block-subpages'
    )
  );
}