Drupal entity with field collections not saving value to a variable - php

I have field collections within an entity and I'm trying to theme the values but having a hard time saving some of those values as variables. In the code below, I am trying to print the value for 'field_area_headline' but getting an 'undefined index' notice in my browser. I've included an image of my browser where I have dpm-'d my variables that will likely be a clue for those who know what to look for.
What am I doing wrong? Thanks in advance!
<?php foreach($variables['field_focusareas'] as $delta => $item) : ?>
<?php $focus_area_node = $item['entity']; ?>
<?php foreach($focus_area_node->field_area as $focus_delta => $area) : ?>
<!-- <?php dpm($area); ?> -->
<?php $focus_area = entity_load('field_collection_item', array($area[0]['value']));
dpm($focus_area);?>
<?php $headline = $focus_area['field_area_headline'][LANGUAGE_NONE][0]['safe_value']; ?>
<h3><?php print $headline; ?> </h3>
<span></span>
<?php endforeach; ?>
<?php endforeach; ?>

$focus_area is an object, not an array. You need to retrieve an object property - $headline = $focus_area->field_area_headline[LANGUAGE_NONE][0]['safe_value'];

Related

How to work with if not in PHP?

I want to print the div with the class called inner-content-div, if the variable $name is not consisted with the following strings.
Gingelly Rolls
Kithul Treacle
Coconut Vinegar
But my PHP code is not working.
Here is my code.
<?php
$vid = explode("/", $_GET["q"]);
$name = taxonomy_term_load($vid[2]);
?>
<h1 id="page-title" class="title"><?php print $name->name; ?></h1>
<?php
$exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
if(!in_array($name, $exclude_list)){ ?>
<div class="inner-content-div">
<!-- Here are some HTML code.-->
</div>
<?php
}
?>
It should be -
if(!in_array($name->name, $exclude_list)){
as you are printing it -
<h1 id="page-title" class="title"><?php print $name->name; ?></h1>
<?php
$vid = explode("/", $_GET["q"]);
$name = taxonomy_term_load($vid[2]);
$exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
?>
<h1 id="page-title" class="title"><?php echo $name->name; ?></h1>
<?php if(!in_array($name->name, $exclude_list)): ?>
<div class="inner-content-div">
<!-- Here are some HTML code.-->
</div>
<?php endif; ?>
It could be because $name is an object. Also, probably better to use PHP's alternate syntax for cleaner html/templates.
The other potential issue here is that the casing of the items in the exclude list could potentially not match the value of name. You should probably normalize those to all lower case when doing the comparison.
you can try like this. First print $vid[2], using echo $vid[2];. Then put $vid[2] values into the array called $exclude_list. Then use the following code.
$exclude_list = array(//value 1, value 2, value 3);
if(!in_array($vid[2], $exclude_list)) {
//Your HTML code
}

Hide Div title if there's no content

I'm very newbie regarding PHP, I'm trying to configure my wordpress Loop.
I figured out how to display my customfield in my template, I manually added a title before my taxonomy and custom fields, but I'd like it doesn't show if the taxonomy is empty.
Here is the code:
<div class="customfields">
<h2 class="widgettitle">My Title</h2>
<?php echo do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
I would very much appreciate your help!
Thanks a ton,
T.
So code should be
<?php $taxonomy = do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<div class="customfields">
<?php if(!empty($taxonomy)) : ?>
<h2 class="widgettitle">My Title</h2>
<?php echo $taxonomy; ?>
<?php endif; ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
If you want to show content in HTML only if certain variables contain a value then you can create a simple if statement that uses PHP's isset function to determine whether or not the variables are set, and if true place some HTML on the page. You can read more about isset here.

Encoding html entities in url string

I'm trying to prepare a url that has special characters in it, to be used as GET variables in a php file. I think I need html entities and urlencode, which I then need to decode in the other php file. But I'm running into some problems with correctly encoding them.
This is what I have:
<?php $title = "This is a ‘simple’ test"; ?>
<?php $titleent = htmlentities($title); ?>
<?php $titleentencoded = urlencode($titleent); ?>
<?php $date = '21-12-2011'; ?>
<p>Title: <?php echo $title; ?></p>
<p>Title html entities: <?php echo $titleent; ?></p>
<p>Title encoded: <?php echo $titleencoded; ?></p>
<p>Go!</p>
The $titleencoded variable turns out to be empty. I'm overlooking something obvious but I can't see it. What am I doing wrong?
EDIT: New code after suggestions
Okay, so here's what I came up with:
<?php $title = "This is a ‘simple’ test"; ?>
<?php $titleentencoded = urlencode($title); ?>
<?php $htmlent = htmlentities($titleentencoded); ?>
<?php $date = '21-12-2011'; ?>
<p>Title: <?php echo $title; ?></p>
<p>Title encoded: <?php echo $titleentencoded; ?></p>
<p>Title html entities: <?php echo $htmlent; ?></p>
<p>Go!</p>
Is this the right way?
Your variable is empty because you have a typo. You initialize $titleentencoded but later use $titleencoded:
<?php $titleentencoded = urlencode($titleent); ?>
// Should be
<?php $titleencoded = urlencode($titleent); ?>
See #Quentin's answer for a logic error.
You are doing it backwards.
You are putting data in a URL, then a URL in an HTML document.
You need to urlencode the data, put it in the URL then htmlencode the URL and put it in the document.

Don't display if attribute is string(0)

I'm using the following section of code to display some icons on our Magento store with the idea being if there is nothing added in the icons section it shouldn't display, for some reason this isn't working...it is displaying a division as if something is there but there is actually nothing.
<?php
if($_helper->productAttribute($_product,($_product->geticons()), 'icons') !== null):
?>
<div class="product-icons">
<?php echo $_helper->productAttribute($_product,($_product->geticons()), 'icons') ?>
</div>
<?php endif; ?>
It needs to show Icons if they are coded in the attribute field and then hide the division if there is nothing added.
I've worked out that the code is returning a value of string(0) what do I need to change in my coding to get the desired effect?
Here's the thing you need, and you don't need call the same functionality twice to get the empty result. Define your variable and check if it is empty (null, undefined or false) or not
<?php $icons = $_helper->productAttribute($_product,($_product->getIcons()), 'icons');?>
<?php if(!empty($icons)):?>
<div class="product-icons">
<?php echo $icons;?>
</div>
<?php endif;?>
this could be even better solution as it wont call the helper unless there are icons defined but you first have to try it out on your codebase.
<?php if($_product->getIcons()):?>
<div class="product-icons">
<?php echo $_helper->productAttribute($_product,($_product->getIcons()), 'icons') ?>
</div>
<?php endif; ?>
and please check if it isn't a typo and it really is:
$_product->geticons()
or should it be
$_product->getIcons()
Something like:
<?php
if($_helper->productAttribute($_product,($_product->geticons()), 'icons'))
{
echo "<div class=\"product-icons\">";
echo $_helper->productAttribute($_product,($_product->geticons()), 'icons');
echo "</div>";
}
?>
would be better!
try
<?php
if(!empty($_helper->productAttribute($_product,($_product->geticons()), 'icons')))
{
echo "<div class=\"product-icons\">";
echo $_helper->productAttribute($_product,($_product->geticons()), 'icons');
echo "</div>";
}
?>

Magento: Getting Product Url's for Products within a Grouped Product

For a grouped product, I would like to display a link to the simple products it is composed of. For example, if I have a grouped product called Dining Set composed of plates, knives, forks, etc. I'd like each of the subproducts to have a link to that subproduct (click plates goes to the Simple Product for plates)
<?php foreach ($_associatedProducts as $_item): ?>
<tr>
<td><?php echo $this->htmlEscape($_item->getName()) ?></td>
<td class="a-right">
<?php echo $this->getPriceHtml($_item, true) ?>
</td>
<?php if ($_product->isSaleable()): ?>
<td class="a-center">
<?php if ($_item->isSaleable()) : ?>
View
<?php else: ?>
<p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock.') ?></span></p>
<?php endif; ?>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
This is a code snippet from the grouped.phtml file in
app/design/frontend/blank/default/template/catalog/product/view/type/grouped.phtml
In particular the line that has $_item->getProductUrl(),
this does not work, and I don't know the code needed to get the url for this associated product item. If anyone could help here it would be much appreciated.
Also, where on earth can I find the method's available (and how they're used) for Products or Categories or $_item and the like?
Easy to find all methods and functions. Always trace back to the Core /app/code/core/Mage/Catalog/Model/Product.php or any of the other files in that Folder.
Your code is perfect. Just use
$_item->getUrlPath() ;
instead of productURL.
Just a few notes on getting the available methods / data:
First, to get all methods actually coded into the classes, you can get all the available methods with:
$array = get_class_methods($_item); //yields an array of the methods in the class
var_dump($array); // to see the methods
To get all data-related methods, first find out the data members in the class. This works with most objects in Magento:
$data = $_item->getData(); // $key => $value array
Then you can get any piece of data you want two ways:
// assuming I want 'my_data'
$data = $_item->getMyData();
$data = $_item->getData('my_data');
<?php echo $this->htmlEscape($_item->getProductUrl()) ?>
or here is the whole A HREF:
<a href="<?php echo $this->htmlEscape($_item->getProductUrl()) ?>">
<?php echo $this->htmlEscape($_item->getName()) ?>
</a>

Categories