drupal 7 Targeting specific arrays in a page.tpl.php - drupal-7

I have a situation where i need two different conditions to be met in order for the title of a given node to be printed to the page (this bit below is from my page.tpl):
<?php if
($title && ($user->uid == 1) || $title && ($page['#object']['type']['blog_post'])
/* is the node a full blog post? */
): ?>
I want to display the title of a node only if the user is logged in. which is working, but i am not sure how to display the title for my specific node type. Obviously I am drilling down into the wrong part of the array. But I can't seem to figure out which part of the array will deliver to satisfy my condition.
thanks!

You can use $node variable in your page.tpl.php when diaplayed page is node. Node type stores in $node->type. But only for node pages. Node page of type 'blog_post' displays full blog post.
<?php if(isset($node) && $node->type == 'blog_post');?>
<?php print $title; ?>
<?php endif?>
In page.tpl will print title only on full nodes of your type.

You can use node.tpl.php for handling specific node parameters,
which you'll find node.tpl.php in /modules/node/node.tpl.php
The node template offers you parameters for checking the view mode ($view_mode) and $title for that node.
P.S., if you want to check if your user is logged in, drupal offers you a function user_is_logged_in()

Related

For Each instruction based on a dynamic array

I'm currently experiencing the following problem:
For Each item In Array
If isItemInTheArray(Array, item) Then
removeItem(Array, item)
End If
Next
But the problem is, when removing the current item (i.e. index = 0), the next item which should be taken by the for each is still index 0 and not the index 1.
To sum up, in vb.net, how to tell to "for each" that the next item it should take is still the first one (i.e. index 0) and not the obvious next one (i.e. index 1)?
EDIT:
Maybe I could be more specific.
I have many pieces of music in my drive which are stock like following:
Each .mp3 file is in a folder like "Blob-release2018\blob.mp3"
Each music folder contains different types of file like ".mp3", ".nfo" or ".jpg"
Some music has 2 version : "extended" for DJing and "edited" for radio broadcasting
Some music folder contains albums, so different .mp3 but not the same music...
I want to scan my all music path and populate a grid view with ".mp3" files. But if I have 2 version of a song, I need only 1 line with a "version extd" / "version edit" checkbox checked to show which version I have....
EDIT: Problem solved by using a custom class and check before adding in a collection. Thanks to #Craig ! More details in comment zone

How to set a block for dynamic node in DRUPAL 7

I am new to Drupal. I have created a block that I want to show on some specific pages. Like if the url is "/node/2278" then I want to show the block for all the page under this "/node/2278". This may be /node/2278/1 or /node/2278/any-number.
You can use the Followings Path configurations
using (node/2278) -> the block will display only on node/2278.
using (node/2278/*) -> the block will display on node/2278/12 where * can be any text or number
using (node/2278/*/*) -> the block will display only on node/2278/12/45 where * can be any text or number
Use "/node/2278/*" , I think it actually tells you that the star can be a wildcard under the textfield where you enter it.
Don't forget you can set up url aliases so the path does not have to be "node/..." but can be whatever you want (admin/config/search/path/patterns)

CakePHP HtmlHelper Close Tags

Can someone explain why this is useful if you can never use the HtmlHelper to close a tag?
// No text specified.
<?php
echo $this->Html->tag('span', null, array('class' => 'welcome'));
?>
// Output
<span class="welcome">
In an ideal world there would be some way to create a closing tag, such as </span>
Well, the "usefulness" depends on how it's been used.
First, there's no HtmlHelper->closeTag function, because the tag function surrounds the string (or element) that's inside. Example
echo $this->Html->tag('span', "i'm a span", array('class' => 'welcome'));
// Output
<span class="welcome">i'm a span</span>
There's really no point on having a closeTag function if the HtmlHelper opens and closes tags inside the function.
Clearly an unclosed <span> tag isn't that useful either, and that example isn't the best one to show some use (I know it's in the cake docs, not blaming you).
Since HTML isn't XML, then there are cases where unclosed tags may appear (check the HTML specs). In that link is a list of what is defined as void elements, which are:
area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr
Little ways down that link reference, it is written
Void elements only have a start tag; end tags must not be specified for void elements.
Soooo.... you may want to do that. You can write <br> with a complicated and more character-count expensive command. Maybe you want to write the whole .cpt view in php and no echos of normal strings, just pure php with the advantages of HtmlHelper (like escaping html characters).
So, in theory, you can do
echo $this->Html->tag('br', null, array());
echo $this->Html->tag('link', null, array());
and that would be valid tags. Why would you do that? * shrug * Who knows. But isn't it nice you have the option?
Also, HtmlHelper probably uses that function internaly to write other tags. I mean, maybe HtmlHelper->img() uses HtmlHelper->tag('img', null), and since the tag function is public, well, you get that seemingly useless option as well.
I know it doesn't sound as much "usefulness" still, but it's the little things that count...

Print only the value of a field in Form

I would echo the value of the fields of a Model in cakePhp, without creating a Form.
Is it possible with core Helper or I have to build a custom one?
In particulare, if I use
echo $this->Form->input('field',array('label'=>"This is my value:"))
it prints out an input with a label.
What I want is something like:
echo $this->Form->label('field',array('label'=>"This is my value:"))
and it shall print out
"This is my value: <value_of_field>"
All the values that the FormHelper uses are from $this->request->data, so, try printing echo $this->request->data[YourModel][field];
If not, then you can define a view variable in the controller via $this->set('variableName', variableValue); and assign it in the form field.

How to get the last of each entity in the GAE datastore that starts with 'A', that starts with 'B', etc

I have a GAE datastore with 303 Game() entities.
class Game(db.Model):
title = db.StringProperty(required = True)
slug = db.StringProperty(required = True)
category = db.CategoryProperty()
description = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
The 'slug' property is similar to the title except that it replaces all whitespace with a hyphen and removes all special characters. ex: ('The First Game!' becomes 'the-first-game')
On the website I am listing all 303 games using the Bootstrap collapse accordion. It works fine, however because there are so many games, I attempting to setup a side nav that lists the letters of the alphabet. The user would click on one of the letters to jump to the specific position of the list. ex: (User clicks 'N', page jumps to games that start with 'N').
I know this works, because if I hard code the 'slug' into the HTML it will correctly jump to desired location. However, the 'slug' needs to be one before the correct 'slug' or it won't jump correctly. ex: (User clicks 'N', the slug in the HTML needs to be 'my-last-m-game' to jump to the correct position, and display 'N' games at the top of the page).
My question (sorry for the long winded explanation) is, is it possible to do a GQL query to get the last game for each letter of the alphabet? Or is it better to put all the games in a list/dictionary and get the correct 'slug' that way? Or should I add a property to the data itself, that allows me to GQL search just for the 'A' games or just for the 'T' games, to get the correct 'slug'. Or is there an offset I could use in Bootstrap, that would allow me to get the first 'N' game 'slug' and use that to jump to the correct position on the page?
Thanks for any help you can offer. It's my first time posting to stackoverflow, apologies if I'm not doing it correctly.
Another option is to have a field on Game called lastGameForLetter )which is set to null for most games). When you insert a game, it will require an extra check to make sure this doesn't need to be updated but makes the querying much easier. We do things like this all the time for things that are written rarely but read often.
You can use prefix-match on strings. u"\ufffd" is the highest possible unicode-character, so querying by name < u"b"+u"\ufffd" and sorted by name should do the trick for elements starting with "b". Please note that such a filtering/ordering is case-sensitive, so you might need to store an additional field in lowercase for this.

Resources