change position of the flash message - cakephp

I have something like this:
controller.
public function add() {
$this->layout = 'home';
$this->Session->destroy();
if ($this->request->is('post')) {
$this->Session->setFlash(__('xxx', true));
}
}
view. home.ctp
<div id="container">
<div id="header">
</div>
<div id="content">
<?php echo $this->fetch('content'); ?>
</div>
<div id="footer">
<?php echo $this->Session->flash(); ?>
</div>
</div>
My question is: Why the flash message is not showed in the footer? well, what I want is show the message below the form, but to simplify the problem, with this code the message is also showed in the top. But should be in the footer. Also if i delete
<?php echo $this->Session->flash(); ?>
the message is also showed on the top of the page.
So, how can i change the position of this flash message?
Edit:
<?php var_dump($this->fetch('content')); ?>
output:
string '<div id="flashMessage" class="message"> The user xxxxx</div>
<div class="users form">
<form action="/cake/users/add" id="UserAddForm" method="post" accept-charset="utf-8"><div style="display:none;"><input type="hidden" name="_method" value="POST"/></div> <fieldset>
<legend>
Add User
</legend>
<div class="input text required error"><label for="UserUsername">Username</label><input name="data[User][username]" maxlength="50" type="text" value="" id="UserUsername" class="form-er'... (length=1187)

You are setting the layout to 'home', is that right? Did you create a home.ctp under views/layouts? I ask you because the view that you show looks like the default layout shipped with Cake (default.ctp).
For me, the code looks fine mate, just be sure you are displaying the right layout.

When I delete my <?php echo $this->Session->flash(); ?> I don't get flash messages. I think you should consider #clapas's answer.

Related

CAKEPHP 3: Set a value for the default layout. This becomes undefined when fetching content, why?

First let me say thanks in advance for your help. I am new to CakePHP framework and I am trying to learn this fast due to a project I’ve been thrown into.
I think my problem might be due to not fully understanding how the views are working. This is how I think it works. (please correct me if I am wrong): The default layout is a template. $this->Fetch() is kind of like ajax where by only the div containing the element is re-rendered/updated?
my basic html default template is:
<!DOCTYPE html>
<html>
<head>
<?= $this->Html->charset() ?>
<?= $this->fetch('meta') ?>
<?= $this->fetch('css') ?>
<?= $this->fetch('script') ?>
</head>
<body>
<nav class="top-bar expanded" data-topbar role="navigation">
<ul class="title-area large-3 medium-4 columns">
<li class="name">
<h1><?= $this->fetch('title') ?></h1>
</li>
</ul>
<div class="top-bar-section">
<?= $this->Element('Navbar/navbar'); ?>
<?= $this->fetch($navigation); ?>
</div>
</nav>
<?= $this->Flash->render() ?>
<div class="container clearfix">
<?= $this->fetch('content') ?>
</div>
</body>
</html>
in the default layout:
<div class="top-bar-section">
<?= $this->Element('Navbar/navbar'); ?>
<?= $this->fetch($navigation); ?>
</div>
I set the value of $navigation in my UsersController to:
$this->set('navigation', 'navbar2');
This calls the element (Element/Navbar/navbar.ctp)
<?php $this->start('navbar2'); ?>
<ul class="nav navbar-nav">
<li><?= $this->Html->link(__('Session history'), ['controller' => 'Sessions', 'action' => 'index']) ?></li>
</ul>
<?php $this->end(); ?>
the result is (web page view):
navbar
Which is what I wanted.
However, when I click on Session history from /users/home to /sessions this happens:
undefind var
So either two things are happening, somehow the fetch(‘content’) is causing $navigation to fade out of scope and become undefined or I’m completely wrong about $this-fetch() and what is really happening is the whole default page is reloaded fresh.
I guess my first question is, which one is correct? Ajax style fetch content without refreshing the whole page, or is it refreshed every time and $navigation is a completely new variable that has not been assigned.
In either case, how do I set the default layout values that will only change when an event occurs? In my example, navbar2 is for non-admin, while navbar1 is for admin. I only need to check this once when logging in.

How to set checkbox label position without using formMultiCheckbox(...) in ZF2?

I want to be able to set the label position:
for the entire element and
(in case of checkboxes / radio buttons) additionally for every single checkbox / radio button.
I have a form, that is build like this:
<?php
$form = $this->form;
$form->setAttribute('action', $this->url());
$form->prepare();
echo $this->form()->openTag($form);
?>
<?php $fooFieldset = $form->get('foo'); ?>
<div id="fieldset-foo" class="fieldset-container col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><?php echo $this->translate($fooFieldset->getLabel()); ?></h3>
</div>
<div class="panel-body">
<?php foreach ($fooFieldset as $element) : ?>
<div class="form-group <?php if($this->formElementErrors($element)) echo 'error' ?>">
<?php $element->setOption('label_position', FormRow::LABEL_PREPEND); /* not working! */ ?>
<label><?php echo $this->translate($element->getLabel()); ?></label>
<div>
<?php $element->setAttribute('class', 'form-control'); ?>
<?php echo $this->formElement($element); ?>
<?php if ($this->formElementErrors($element)) : ?>
<div class="message-error"><?php echo $this->formElementErrors($element) ?></div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<!-- futher fieldsets -->
<!-- fields without fieldsets -->
<?php
echo $this->form()->closeTag($form);
?>
That means, I'm not calling the formRow(...) or formMultiCheckbox(...) and cannot configure the label_position on this way. The setting an option directly on the element ($element->setOption('label_position', FormRow::LABEL_PREPEND)) does not work at all -- neither for the entire element nor for checkboxes / radio buttons.
How to get it working and set the label position (1. for the entire element; 2. for the checkboxes / radio buttons) here?

Drupal 7 displaying content issue

How to hide a specific field of basic page content type? Not from admin pannel Structure>content type>basic page> manage display.. I want to hide it in my page template then showing separatly so that i can customize it?
In your template, you can use the hide function to prevent fields from being rendered.
Here's an exampled from the Garland theme's node template:
<div class="content clearfix"<?php print $content_attributes; ?>>
<?php
// We hide the comments and links now so that we can render them later.
hide($content['comments']);
hide($content['links']);
print render($content);
?>
</div>
<div class="clearfix">
<?php if (!empty($content['links'])): ?>
<div class="links"><?php print render($content['links']); ?></div>
<?php endif; ?>
<?php print render($content['comments']); ?>
</div>

How to set request data in controller

I have to set request data which is in the form of HTML in CAKEPHP.
Request Data is :
<div class="row">
<div class="span6">
<h3>Company Name</h3>
Company Address
</div>
<div class="span6">
<h5>INVOICE</h5>
</div>
</div>
How to set this in controller? $this->set() is not working.
$this->set('html', $this->request->data);
View :-
<?php echo $html; ?>
In case you are talking about data to fill form
$this->request->data = $this->YourModel->find();
should do the trick.

How to get controller view inside an element?

I have created an element having a text field(Status Bar same as Facebook) which i extended in home/display view.
Code for element:
<?php
echo $this->Html->css('notes');
echo $this->Html->script(array('jquery','jquery.elastic','notes'));
?>
<div class="notescontainer">
<div class="share">Share:</div>
<div class="notes">Notes</div>
<div class="loading"></div>
<div class="img_top"></div>
<div class="text_status">
<?php
echo $this->form->create('Note', array('action' => 'notes'));
echo $this->form->input('notes',array('class' => 'notesbar', 'label'=>'', 'placeholder' => 'Write down you notes ...'));
?>
</div>
<div class="button_outside_border" id="share">
<div class="button_inside_border">
<?php
echo $this->form->submit('Share',array('alt' => 'Add', 'class' => 'sharebutton'));
echo $this->form->end();
?>
</div>
</div>
<div class="clear"></div>
<div class="load_status_out"></div>
</div>
I want that when user shares the status it should re-appear on the same page that is display/home and for that i created a controller for retrieving the status entered by the user.
Controller:
<?php public function notes()
{
$this->set('allnotes', $this->Note->find('all'));
}?>
Controller View:
<?php
foreach($allnotes as $viewnotes):
{
echo $viewnotes['Note']['notes'];
echo "<br>";
}
endforeach;
?>
Now i want this controller view to be displayed on home/display view. Is there any way i can extend it in that element? I don`t want to add it directly on my home/display view as it already fully loaded.
You can use requestAction() to accomplish this, but in my experience it is buggy and a sign of design failure if you need it.
Alternatively you can turn all your view into elements, which can be embedded in each other in any arbitrary way. Then you can just call some shared code in the controller to populate all required models.

Resources