Wrap output with link from field in drupal views - php

I have a view that is outputting a list of nodes that contain a link, image and text.
I want to use the link field to wrap the output (rather than link to the node), but I can't figure out how to get the raw url/title etc to create create that link in the template.
The field is configured to output the url as plain text, but is wrapped in div/spans regardless of the style settings for the view field.
views-view-fields--my-view.php:
<a href="<?php echo $fields['field_link']->content ?>">
<?php foreach ($fields as $id => $field):
if ($id == 'field_link') continue;
?>
<?php if (!empty($field->separator)): ?>
<?php print $field->separator; ?>
<?php endif; ?>
<?php print $field->wrapper_prefix; ?>
<?php print $field->label_html; ?>
<?php print $field->content; ?>
<?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>
</a>
This produces:
<a href="<div class=" data-thmr="thmr_72"><div class="field-items"><div class="field-item even"><span data-thmr='thmr_24' class='devel-themer-wrapper'>/drupal/%237digital-buy</span></div></div></div>">
[...]
</a>
Which is obviously not what I need.

Found the solution. You need to rewrite the output. See below.

Related

Display MySQL request in PHP

I am trying to make a basic todolist but i want to display a list with task undone at the top and the done at the bottom.
Every time i try i have a blank page
<h1 class="header">To do.</h1>
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<?php if ( $item['done'] == 0 ) {
echo '<li>';
echo '<span class=\"item'?><?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> No tasks</p>
I have a table items with a field Done (0,1)
I have tryed to put if condition in the foreach but it failed.
Thanks for the help
At the end of your line which begins with echo '<span class=\"item'?> you set a PHP open tag but without closing it and then open another one on the next line:
<?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
It looks like you meant to close the PHP tag on the previous line with ?>.
As a side suggestion, if you use a good editor or IDE it will show you errors on front of you as you type, saves a lot of time.
Viewing your PHP error logs is a must as a developer, as it tells you of problems as you work, and this would have shown a fatal error and the line number etc.
A white page is also a sign that you have a fatal error as PHP crashes before it will output any content to the screen.
When i remove the if
The code is working
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<li>
<span class="item<?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; ?> </span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> You have no task</p>

Yii2: how to display images from backend/web to frontend in advanced app

I need to display 1 (first) image which path is saved in database like Img1.jpg;img2.jpg;, I tried to seperate each path by using explode, getting all the images as array but not able to pick single path-
<div class="col-sm-6 masonry-item">
<a href="<?php echo Url::to(['site/roompage']); ?>" class="product_item text-center">
<span class="product_photo bordered_wht_border">
<?php
foreach (explode(';',rtrim($row['images'],';'),1) as $key_img => $value_img)
{
?>
<?php echo Html::img('#backend/web'.'/'.$value_img);?>
<?php
}
?>
</span>
<span class="product_title"><?php echo $row['room_type']; ?></span>
<span class="product_price">Rs.<?php echo $row['rate']; ?></span>
</a>
</div>
<?php endforeach; ?>
<?php
// if you want only the first image is better
$my_image = explode(';',rtrim($row['images'],';'),1);
echo Html::img('#backend/web'.'/'.$my_image[0]);
}
?>
otherwise
<?php
// if you want all the image
foreach (explode(';',rtrim($row['images'],';')) as $key_img => $value_img)
{
echo Html::img('#backend/web'.'/'.$value_img);
}
?>
this because (explode(';',rtrim($row['images'],';'),1) return an array of two element (the first and the rest)
The src parameter, containing the backend alias, will be processed by Url::to()
Check the docs for details on Html::img()

PHP echo as condition inside PHP if statement

I would like to use a PHP echo as a condition inside a PHP if statement.
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this case, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
What could be an option is to save all author within an array
// if article->author() isn't within the array
$authors[] == $article->author();
After that you could go as the following:
<?php foreach($authors as $author){ ?>
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
<?php } ?>
That should work, even if you must do 2 foreachs
<?php if( $article->author() == $page->author() ) { ?>
<p><?php echo $article->title(); ?></p>
<?php } ?>
should work, but you can also try
<?php
if( $article->author() == $page->author() ) {
echo "\n<p>", $article->title(), "</p>\n";
}
?>
which to me looks "cleaner"; but you'd have to have a look for missing whitespaces
I suggest trying to set it equal too a variable and then using that variable in the if statement.
<?php foreach(page('magazine')->children() as $article): ?>
<?php $condition = $page->author()?>
<?php if($article->author() == $condition ?>'): ?>
echo "\n<p>", $article->title(), "</p>\n";
<?php endif ?>
<?php endforeach ?>
I am not sure if my syntax is correct but i think it is something along them lines.
You cannot use echo in condition because it is special language construct that sends given contents to the output stream and it returns no value.
Are you sure you shouldn't have this?
<div id="automatic_list_of_articles">
<?php $page = page('magazine'); ?>
<?php foreach($page->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
I have reconstructed an approximation of what looks to be your data, and you can see it working at the link below. It correctly echo's multiple article titles.
Working example:
http://ideone.com/jvLVhF
In this example you can see the PHP as above works correctly, and it is likely a data issue (ie. you should perhaps be using $page and not calling a function in the foreach statement).

Editing a wordpress loop to bring in one page via its ID number

This loop brings in all the pages and shows it on a single page. How can I edit it so that I only bring in one page identified by its ID? (the page wont change)
Thanks
<section id="<?php echo $post->post_name;?>" class="page-area<?php echo $bgClass;?>"<?php echo $style;?>>
<div class="wrapper"<?php if($fullEmbed<>''):?> style="width:100%"<?php endif;?>>
<?php if($hideTitle!='Yes'):?>
<hgroup class="title">
<h1<?php echo $font;?>><strong><?php echo $mainHeading;?></strong></h1>
<?php if($subHeading<>''):?><p<?php echo $font;?>><?php echo $subHeading;?></p><?php endif;?>
</hgroup>
<?php endif;?>
<?php if($fullEmbed<>''):?>
<div class="full-embed"><?php echo van_shortcode($fullEmbed);?></div>
<?php else:?>
<div class="entry"<?php echo $font;?>>
<?php van_content(true,true);?>
</div>
<?php endif;?>
</div>
</section>
<?php endwhile;?>
You mean like:
<?php
$page_id = 123; // 123 should be replaced with a specific Page's id from your site, which you can find by mousing over the link to edit that Page on the Manage Pages admin page. The id will be embedded in the query string of the URL, e.g. page.php?action=edit&post=123.
$page_data = get_page( $page_id ); // You must pass in a variable to the get_page function. If you pass in a value (e.g. get_page ( 123 ); ), WordPress will generate an error. By default, this will return an object.
echo '<h3>'. $page_data->post_title .'</h3>';// echo the title
echo apply_filters('the_content', $page_data->post_content); // echo the content and retain WordPress filters such as paragraph tags. Origin: http://wordpress.org/support/topic/get_pagepost-and-no-paragraphs-problem
?>
Straight from the WP Codex http://codex.wordpress.org/Function_Reference/get_page

Filtering out items using SimpleXML

Been looking around and am stumped. Basically I'm trying to filter out a result from an xml feed and hide it from displaying in the html output. I'm trying to find out the venue "Hornblower Cruises and Events" and if it exists, hide the whole , which is the parent of all the nodes.
Here's the URL: http://www.sandiegomagazine.com/goldstartest/rss3-exclude.php
Here's my code:
<?php
$myfeed = simplexml_load_file('https://www.goldstar.com/api/listings.xml?api_key=6d0d598c69dfecfcf028b0d2b3c9b693b606ad8c&postal_code=92101');
$i = 0;
foreach ($myfeed as $goldstar):
$title=$goldstar->headline_as_html;
$summary=$goldstar->summary_as_html;
$dates=$goldstar->upcoming_dates->event_date->date;
$ourprice=$goldstar->our_price_range;
$fullprice=$goldstar->full_price_range;
$img=$goldstar->image;
$link=$goldstar->link;
$venue_name=$goldstar->venue->name;
$venue_street=$goldstar->venue->address->street_address;
$venue_locality=$goldstar->venue->address->locality;
$venue_region=$goldstar->venue->address->region;
$venue_zip=$goldstar->venue->address->postal_code;
$venue_phone=$goldstar->venue->phone;
$category=$goldstar->category_list->category->name;
// if ($venue_name == 'Hornblower Cruises and Events'){
// unset($myfeed->event);
//echo $myfeed->asxml();
//}
if (++$i > 20) {
// stop after 10 loops
break;
}
?>
<html>
<head></head>
<body>
<div class="gs-item">
<div class="gs-itemcontent">
<h3 class="gs-cat"><?php echo $category; ?></h3>
<h2><?php echo $title; ?></h2>
<h4 class="gs-date">Date: <?php echo $dates; ?> <br/>For more show dates, click here</h4>
<img src="<?php echo $img; ?>" />
<p><?php echo $summary; ?></p>
<div id="gs-callout">
<span class="fullprice">Full Price: <?php echo $fullprice; ?></span>
<br/>
<span class="ourprice">Our Price: <span class="gs-hilite"><?php echo $ourprice; ?></span></span>
<p><a class="gs-button" href="<?php echo $link; ?>" target="_blank">Buy Tickets ยป</a></p>
</div>
<ul class="gs-venue">
<li><strong><?php echo $venue_name; ?></strong> | </li>
<li><?php echo $venue_street; ?></li>
<li><?php echo $venue_locality; ?>, <?php echo $venue_region; ?> <?php echo $venue_zip; ?></li>
<li><?php echo $venue_phone; ?></li>
</ul>
</div>
<div class="gs-clear"></div>
</div>
<? endforeach; ?>
</body>
Help?
Use a keyed foreach
foreach ($myfeed as $key => $goldstar):
And then unset the entire current xml using the key
unset($myfeed->$key);
Or unset just the venue with unset($myfeed->$key->venue);
Alternately, you could just build a new obj instead of trying to edit the existing one. Instantiate a new object before your loop and then only add the pieces to it that you want to keep.
Or, you can just continue the foreach if you find the unwanted venue. The only downside is that you won't have a copy of your ok'd list in $myfeed. So...
if ($venue_name == 'Hornblower Cruises and Events') { continue; }
Firstly you should cast every object that you are getting from your SimpleXML object to the relevant type. For example you should cast string attributes or nodes with putting (string) before reading the field:
$venue_name = (string)$goldstar->venue->name
For your question about filtering, I'd suggest your own way to keep looking for the match string and then decide to add or not.
EDIT
You're trying to see if the venue name is 'Hornblower Cruises and Events' then unset the current event field and echo it as XML? Firstly you should write unset($myfeed) because it's the event field itself. Next asxml would return nothing if you unset $myfeed. And you have HTML mistake in your code. In every loop of your for, you're writing:
<html>
<head></head>
<body>
You should place them before the for. And there is no closing tag for them. Put the closing tags after for. If you want to just get the fields with not venue name 'Hornblower Cruises and Events', put the <?php if($venue_name != 'Hornblower Cruises and Events') : ?> before <div class="gs-item"> and put <?php endif; ?> after </ul>.

Categories