Dear Drupal users,

My knowledge about php is close to 0.
I've bought a Drupal 7 theme and started working on it. Now I'm facing a problem, but the theme designer doesn't support me with this issue, cause he says its not theme related.

In my theme sub folder is a file named 'portfolio.inc' It contains these lines (and more coding):

		  <div class="gallery_item">
		    <div class="gallery_item_padding">
          <div class="gallery_item_wrapper">
            <a href="'.file_create_url($item['uri']).'" class="prettyPhoto" data-rel="prettyPhoto[gallery1]" title="Title">
						  <img class="gallery-stand-img" src="'.file_create_url(image_style_path('oyster_portfolio_gallery', $item['uri'])).'" alt="Portfolio Gallery">
						  <span class="gallery_fadder"></span>
						  <span class="gallery_ico"><i class="stand_icon icon-eye"></i></span>
						</a>
          </div>
        </div>  
		  </div>

Where it says 'title', I want the site to print the image title of each image <a href="'.file_create_url($item['uri']).'" class="prettyPhoto" data-rel="prettyPhoto[gallery1]" title="DISPLAY TITLE">

And where it says 'alt', I want the site to print the image alternate of each image <img class="gallery-stand-img" src="'.file_create_url(image_style_path('oyster_portfolio_gallery', $item['uri'])).'" alt="DISPLAY ALT">

Comments

ItangSanjana’s picture

At the very bottom of the file Just before the return $output line of function portfolio_gallery(), append this spell:

  drupal_set_message('<pre>' . print_r($item, TRUE) . '</pre>');

Refresh the page or clear the cache first.
See if there is something related to image title or alt.
If so, just print the value as usual.

  print $item['key']['to']['img_title_or_alt'];

Else, try another variables to inspect.
Happy drupling and HTH...

Michael84’s picture

Thank you, but this code is giving me a blank page.

<?php
  print $item['key']['to']['img_title_or_alt'];
?>

(included to 'portfolio.inc' file.)

ItangSanjana’s picture

My bad, that's just an example, use the actual arrays from the print_r() results :)

Michael84’s picture

[node:field_portfolio_gallery]
[node:title]

I think these are the array's. The first for title, the second for alt.

ItangSanjana’s picture

Try

  drupal_set_message('<pre>' . print_r($item['#item']['title'], TRUE) . '</pre>');

Any results?

Michael84’s picture

Yes, a blank page.

ItangSanjana’s picture

After seeing the whole code, the snippet above should be placed in function portfolio_gallery() just before the return $output line.

Back to my first comment and retry the steps...

Michael84’s picture

A white page with my logo, site name, menu link and search box.

yelvington’s picture

Using print_r($item) or by installing the devel module, you need to find the element that contains text for the alt and title fields -- if indeed those fields are populated at all.

if $item['uri'] contains the uri of the image, then it's likely that the places to look are $item['alt'] and $item['title']. Printing out the structure of the $item array should verify that.

Based on the snippet you posted, it appears that the original author has used single quotes to enclose the HTML as strings, so you would want to do something like this:


<a href="'.file_create_url($item['uri']).'" class="prettyPhoto" data-rel="prettyPhoto[gallery1]" title="' . print $item['title'] . '" alt="' . print $item['alt'] . '">
<img class="gallery-stand-img" src="'.file_create_url(image_style_path('oyster_portfolio_gallery', $item['uri'])).'" alt="Portfolio Gallery">
<span class="gallery_fadder"></span>
<span class="gallery_ico"><i class="stand_icon icon-eye"></i></span>
</a>

Note that at the beginning and the end of this snippet, you're already inside a single-quoted string. This makes for some difficult-to-read code, but if you pay very close attention to the single quotes, you should be able to figure it out.

ItangSanjana’s picture

Good point :)

Michael84’s picture

I'm sorry, nothing worked so far. Unless I did something wrong. This is the full code

<?php

function portfolio_gallery($node) {
  $output = '';
  $output .= '<div class="row portfolio-gallery"><div class="span12"><div class="list-of-images images_in_a_row_3">';
	foreach ($node->field_portfolio_gallery['und'] as $item) {
	
	  // If image file does not exist in image style folder, create it.
		if (isset($item['uri']) && !file_exists(image_style_path('oyster_portfolio_gallery', $item['uri']))){ 
		  image_style_create_derivative(image_style_load('oyster_portfolio_gallery'), $item['uri'], image_style_path('oyster_portfolio_gallery', $item['uri']));
		}
	  
		$output .= '
		  <div class="gallery_item">
		    <div class="gallery_item_padding">
          <div class="gallery_item_wrapper">
            <a href="'.file_create_url($item['uri']).'" class="prettyPhoto" data-rel="prettyPhoto[gallery1]" title="Image Title">
						  <img class="gallery-stand-img" src="'.file_create_url(image_style_path('oyster_portfolio_gallery', $item['uri'])).'" alt="Page Title">
						  <span class="gallery_fadder"></span>
						  <span class="gallery_ico"><i class="stand_icon icon-eye"></i></span>
						</a>
          </div>
        </div>  
		  </div>
		';
	}
	$output .= '</div></div></div>';
	return $output;
}

function portfolio_related_works($nid){
  $count = 1;
  global $base_url;
  $query = new EntityFieldQuery();
	$query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('status', 1)
    ->entityCondition('bundle', 'portfolio');
  $results = $query->execute();
  $output = '';
  $output .= '<div class="row"><div class="span12 single_feature">';
  $output .= '<div class="bg_title"><h3>'.t('Related Works').'</h3></div>';
  $output .= ' <div class="featured_items"><div class="items3 featured_portfolio"><ul class="item_list">';
  foreach ($results['node'] as $result) {
    if ($count == 4) {
	    break;
    }
    if ($result->nid != $nid) {
      // Load those nodes.
      $item = node_load($result->nid);
      
      if (isset($item->body['und'])) {
	      $body = substr($item->body['und'][0]['value'], 0, 75) .'<a class="morelink" href="'.$base_url.'/node/'.$item->vid.'">'.t(' Read more').'</a>';
      }
      else {
	      $body = '';
      }
      
      // Set the $like variable.
      $like = '';
      if (isset($item->field_like['und'])) {
        $like = field_view_field('node', $item, 'field_like');
        $like = drupal_render($like);
      }
      
      $terms = '';
      if (isset($item->field_portfolio_category['und'])) {
	      // Get the portfolio item taxonomy terms.
	      $taxonomy = array();
	      foreach ($item->field_portfolio_category['und'] as $term) {
		      $taxonomy[] = taxonomy_term_load($term['tid'])->name;
	      }
	      $terms = '<div class="preview_categ">'.t('in').' '.implode(', ', $taxonomy).'</div>';
      }
      
	    // If image file does not exist in image style folder, create it.
			if (isset($item->field_image['und']) && !file_exists(image_style_path('oyster_related_works', $item->field_image['und'][0]['uri']))){ 
			  image_style_create_derivative(image_style_load('oyster_related_works'), $item->field_image['und'][0]['uri'], image_style_path('oyster_related_works', $item->field_image['und'][0]['uri']));
			}
			
			isset($item->field_image['und']) ? $image = '<img src="'.file_create_url(image_style_path('oyster_related_works', $item->field_image['und'][0]['uri'])).'">' : $image = '' ;
			
			// Start markup output.
	    $output .= '
	    <li>
	      <div class="item">
	        <div class="item_wrapper">
						<div class="img_block wrapped_img">
							<a class="featured_ico_link" href="'.$base_url.'/node/'.$item->vid.'">
								'.$image.'
								<div class="featured_item_fadder"></div>
								<span class="gallery_ico"><i class="stand_icon icon-eye"></i></span>
							</a>
						</div>
						
						<div class="featured_items_body">
							<div class="featured_items_title">
								<h6><a href="'.$base_url.'/node/'.$item->vid.'">'.$item->title.'</a></h6>
							</div>
							<div class="featured_item_content">
								'.$body.'
								
								<div class="featured_items_meta">
									'.$terms.'
									<div class="gallery_likes gallery_likes_add ">
										'.$like.'
									</div>											
								</div>
							</div>	
					  </div>
				  </div>
			  </div>
			</li>			
			';
		$count ++;	
	  }
	  
  }
  $output .= '</ul></div></div></div></div><hr class="single_hr">';
  return $output;
  
}
neha.gangwar’s picture

Put these two line code inside your foreach loop and share the code you get on your page.

foreach ($node->field_portfolio_gallery['und'] as $item) {

echo('<pre>' . print_r($item, TRUE) . '</pre>');
        exit;
Michael84’s picture

Error message

Notice: Undefined variable: item in require_once() (line 124 of /Users/michael84/Documents/websites/pm/sites/all/themes/oyster/inc/portfolio.inc).

Array
(
[fid] => 413
[uid] => 1
[filename] => web150815-076.jpg
[uri] => public://web150815-076.jpg
[filemime] => image/jpeg
[filesize] => 376685
[status] => 1
[timestamp] => 1446148360
[rdf_mapping] => Array
(
)

[alt] =>
[title] =>
[width] => 1000
[height] => 666
)

The image name is the first file from the lightbox gallery,

ItangSanjana’s picture

Line 124 of portfolio.inc? I see the function only 29 lines?
BTW you've got what you are looking for: ['alt'] and ['title'] :)

Michael84’s picture

I'm giving up. All I get is a white page with the menu links. Time and time again.