Pass data to default.ctp (layout file) from random controller in CakePHP - cakephp

I am simply trying to pass data from my controller to my default layout file:
users_controller
$this->set('fish', 'trout');
default.ctp (layout file)
echo "You caught a " . $fish. " from the river.";
What I am trying to achieve is: echo $group['Group']['name']; in the default layout file, but the above was my first attempt to understand how the relationship actually works.
Thanks for any advise on this : )

anything that you set to the view is available in the layout.

If you set anything in the default.ctp, it will show in the layout just like you would do in a view. There's no difference.
You might not be seeing anything because you have set debug to 0 and you have an error. Try making it 2 and check it out.

As Thorpe says, you need debug set to non-zero.
Check (and change) in app/core.php (search the file for debug and you'll see the information you need).
If you're not getting any output this is most likely the problem.
debug($aVar); is IMO more useful than echo or pr as it will output the line number even if there is no other output (but you must have debug enabled!).

Related

Render individual page fields once only in Drupal 7 page.tpl.php

So I gather I can render a specific element of the $page['content'] array like so...
<?php print render(field_view_field('node', $node, 'field_image')); ?>
Where out of the box this will render the element as expected with standard defaults. Ok, so how can I make sure that hand plucked element no longer renders in the <?php print render($page['content']) ?> call later?
Why do I want something dumb like this? Because every page WILL have a header image with a few css tricks for overlays, design and such. But not every page will have attachments, links, and so on... you know, things that are additional fields in the page. So I can't manually print out each field since I don't know how many or what else there is. All I know for sure is the field_image I'm printing above is wrapped in a ton of markup for styling and must be done this way. Same for a few other fields.
Basically I'm looking for a way to unset the field immediately after use.
Does anyone know how to achieve this? I'd rather not make a view or a custom block that displays for specific pages. I eventually have to hand this over to a client who will not be able to wrap their heads around a single page being administered over many places in the CMS.
You can in fact control the display of individual fields for content types in Drupal without having to resort to the function you've used.
Since you know in advance which field(s) you want to suppress, you can turn off its display in the content type settings.
In Drupal 7, see:
Admin >> Structure >> Content types >> your content type >> Manage display
Under "Format" select < Hidden > for the field that you want to omit.
This will prevent the field contents from being displayed within the usual node contents, but field_view_field will naturally still work.
You can also fine-tune your field formats based on different view modes, e.g. choose to display the field within teasers but not in full content.
Looks like this will work:
<?php
print render(field_view_field('node', $node, 'field_image'));
MYTHEME_remove_item($page['content'], 'field_image');
?>
and in template.php file I made this function:
function MYTHEME_remove_item(&$content, $field)
{
foreach($content['system_main']['nodes'] AS $key => $val){
unset($content['system_main']['nodes'][$key][$field]);
}
}
This is ridiculous, in my opinion. I would think a system as robust as drupal would have a solution for something like this. If anyone knows the proper way to do this I will gladly mark them as correct. In the meantime, for others facing similar situations, this worked for me.

Which controller is setting a flash message?

Is there a way- besides looking over all my code- to find out what controller is setting a flash message?
I have this message that is displaying on all pages, except admin_ pages, which I apparently set at some point during testing, but I would like to get rid of.
I have searched through much of my code for $this->Session->setFlash but I can't seem to find where the problem is.
In short, no. Technically the flash message can be set from anywhere as it's just a session value. Instead, you can debug the location right above the flash message.
In your view, add the debug statement under where the flash message is set.
echo $this->Session->flash();
debug($this->here);

phpQuery behaving weird, changing HTML

I want to process a template with a Google plusone button in it through phpQuery and I run into the following problem:
require_once( "phpQuery.php" );
$p = phpQuery( "<g:plusone></g:plusone>" );
echo $p->html();
the expected output would be:
<g:plusone></g:plusone>
But instead the output of this is:
<plusone></plusone>
Which doesn't match up with what google is expecting, so the button doesn't work any more. How can I stop phpQuery from changing (fixing?) my code, or how can I work around this problem without changing the string from plusone to g:plusone once the processing is done? (that's a nasty workaround, plus, I run into more of these 'translation'-problems in phpQuery).
I had the same problem while including a google+ badge. I could force the code generator from google to generate code with a div container. I just had to tick the "html5 valid code" checkbox.
I've been looking all over for solutions for this problem, to no avail. I found a horrible workaround. I'm sure this is NOT the most elegant way to do this, but at least it fixes the problem.
Instead of the tags:
<g:plusone>...</g:plusone>
use:
<g__0058__plusone>...</g__0058__plusone>
Then simply str_replace the result before you're outputting:
echo str_replace("__0058__",":",$doc->html());
Basically in a tag where you would normally put a colon (:) you put 0058 instead. It's a very non-elegant solution, I realise that, but at least it's a workaround to this old problem.

$session->flash()

I am using cakePHP v1.26.
In the default.ctp file,
I got a single of this code in it:
$session->flash();
I came a corss a web site in which the author suggested using this instead:
if($session->check('Message.flash')){
$session->flash();
}
I do not understand what this line of code is doing:
if($session->check('Message.flash')){...}
what is "Message.flash" in this case?
Is "Message.flash" a custom variable or
a built-in varibale which has been predefined in cakePHP?
Message.flash is the session variable name. It will be defined by cakephp, when you use $this->Session->setFlash('Your message'); from your controller.
if($session->check('Message.flash')){...} checks, if session Message.flash, which contains the flash message, exists.
Note also that contrary to the current manual description, $session->flash() does not echo the result, it just returns it, so you will need to have
echo $session->flash();
in your view.
For latest cakephp version
if(!($this->Session->check('Message.flash')));
// your code
In view section for show messages.
$this->Session->flash();

How to use default.ctp in cakephp

I just finished the "15 min Blog Post tutorial" included in the documentation for cakephp. I was asked for another tutorial to change the layout for first tutorial.
However, I am fairly new to MVC programming/Cakephp and I have no real clue how to do so. Well, I know I need "default.ctp" placed in app/views/layouts/ and I presume I need to include
to include my data? . . .
I am really at a loss of what to do. I set up my default.ctp as I mentioned above, but when I go to localhost:9999/posts the layout is still the same. I guess I need to include a stylesheet (and if so, where?)
I guess if someone can point me in the right direction to a beginner's guide to layout styling or how to use it I would greatly appreciate any help.
I would advice you to read the following from the cookbook: Layouts and CSS. Then copy the layout from /cake/libs/view/layouts/ to /app/views/layouts/ and modify it to your needs. After that create you stylesheet (or modify existing one) in /app/webroot/css/ and include it in your layout.
Create in app/View/Layout a file named "my_posts_layout.ctp"
In your PostController set $this->layout = 'my_posts_layout';
This way you should view the content defined on my_posts_layout.ctp.
Lack of stylesheets has no impact here.
How MVC works in CakePHP:
The router dispatches an incoming request to an appropriate Contoller.
The appropriate Controller function executes (no output, just fetching data, setting up variables).
The appropriate view is rendered. In fact, the output of the view is just contained in $content_for_layout.
What you really get back in the browser is in the layout. Therefore you can put your view's output into the layout by echo $content_for_layout in default.ctp. (Of course you can also have different layouts.) In addition, the layout can be enhanced with elements.
I really recomend the CakePHP CookBook, easily found from the CakePHP homepage. Modifying default.ctp should edit your applications layout.
A more specific question (eg. code samples of your default.ctp, expected results etc) might help people provide a better answer than mine.

Resources