I am using CakePHP 2.3.6. Right now, I am doing a project, where I have several plugins. Now, I want the Elements(header, footer, sidebar) will appear in the default layout according to different plugins. I am trying to explain it using an example :
In my default layout, I have this for the header :
<?php echo $this->element('header');?>
Now, I want to keep the same layout in the "AdminPanel" plugin, so I am not gonna have a different layout in that plugin. But, I want a different header(say, admin-header.ctp) to reaplace that common header(header.ctp), in the same layout, when I call that plugin. Is that possible ? If yes, how ? Please help me.
Thanks.
In your AppController beforeFilter or beforeRender put put something like:
if (isset($this -> params['prefix']) && $this -> params['prefix'] == 'admin') {
$this->set('layout_type', 'admin');
}else{
$this->set('layout_type', 'public');
}
In your default.ctp layout
if('layout_type' == 'admin'){
echo $this->element('admin_header');
}else{
echo $this->element('header');
}
Related
this is my problem...
I have some of images and links that I want to load different joomla modules when user click on them.
mean each hyperlink can load another module|position
thanks all
In case that you just want to call a module's content from a url the following answer will help you.
If you just want to show / hide a module in the same page you could use something similar to my previous answer: Joomla 3 Show different modules on same position depending on toggler
Joomla provides the functionality to call a specific file of the active template by adding the tmpl=FILENAME key/value to the url's query string.
All built-in templates have a component.php file if user wants to load the template with the component only. You could check the following link for more details: Adding print pop-up functionality to a component.
You could do something similar to only show the modules that you want to load.
You could copy the component.php to a new file (I have used custom.php) and added the following php code in the <body> ... </body> part.
<?php
$jinput = JFactory::getApplication()->input;
$selectedPosition = $jinput->getString("position", "");
$selectedModule = $jinput->getString("module", "");
$selectedModuleTitle = $jinput->getString("title");
if($selectedPosition !== "") {
$modules = JModuleHelper::getModules($selectedPosition);
foreach ($modules as $module) {
echo JModuleHelper::renderModule($module);
}
} elseif ($selectedModule !== "") {
$module = JModuleHelper::getModule($selectedModule, $selectedModuleTitle);
echo JModuleHelper::renderModule($module);
}
?>
So with a similar way as loadposition / loadmodule works you could call the new template file using:
index.php?tmpl=custom&position=MODULE_POSITION
or
index.php?tmpl=custom&module=MODULE_TYPE
or
index.php?tmpl=custom&module=MODULE_TYPE&title=MODULE_TITLE
Optionally if you want to load the module with a specific style, you could pass it to the second paramter of the renderModule method like:
echo JModuleHelper::renderModule($module, array("style" => "xhtml"));
Hope this helps
I am trying to set theme dynamically in cakephp 3.x . So I have to check theme exist or not. If exist than I will set otherwise it will get default theme.
if($themeNameExist){
$this->viewBuilder()->theme($themeName);
}
But i did not get any function or way how can I check theme exist or not? Anybody can you help?
In CakePHP 3, themes are plugins, so you could use the functionality for testing whether a plugin is loaded, like
use Cake\Core\Plugin;
if (Plugin::loaded($themeName) === true) {
}
See also API > \Cake\Core\Plugin::loaded()
Depending on why exactly you need to check/set themes dynamically, it might be wise to maintain a whitelist of allowed theme names, as allowing to arbitrary reference plugins (which don't necessarily have to be themes), may to some extent pose a security problem!
$allowedThemes = [
'FooTheme', 'BarTheme', 'BazTheme'
];
if (
in_array($themeName, $allowedThemes) &&
Plugin::loaded($themeName) === true
) {
}
I have find one solution such as where I have checked theme folder exist or not.
use Cake\Filesystem\Folder;
$themeName = 'ThemeName';
$Folder = new Folder(ROOT);
if ($Folder->inPath('plugins' . DS.$themeName)) {
$this->viewBuilder()->theme($themeName);
}
I know that I can replace the flash markup by creating something like custom_flash.ctp in Elements folder and call it like:
$this->Session->setFlash('Hello', custom_flash)
But how can I use custom layout when not adding the second parameter?
$this->Session->setFlash('Hello')
I thought I can replace the default by having a file named default.ctp inside Elements folder. But I can't.
I want to keep the code as short as possible. That's why I'm looking a way to do this
Any solution? Thanks
Try to create your Component:
class MySessionComponent extends Session {
public function setFlash($message) {
return $this->setFlash($message, 'custom_flash');
}
}
and than in your controller just use:
public $components = array('MySession');
$this->MySession->setFlash('Hello');
I found the answer from this question.
We need to add this codes in app/Controller/AppController.php
function beforeRender(){
if ($this->Session->check('Message.flash')) {
$flash = $this->Session->read('Message.flash');
if ($flash['element'] == 'default') {
$flash['element'] = 'fileNameOfYourCustomFlash';
$this->Session->write('Message.flash', $flash);
}
}
}
It basically add element parameter in flash when it doesn't exist yet.
This is explained on the cakephp website here
Is there a way to add microdata to a Drupal 7 site by editing the field.tpl files? My site is built using panels and views so http://drupal.org/project/microdata isn't working. Is there another way to go about adding microdata by hard coding?
Thanks.
The best way to add microdata is by editing the field.tpl files and then checking - use file templates on views. For panels I used the module http://drupal.org/project/panels_extra_styles to add coding around the pane.
I managed to override the page template file and wrap my panel in a code.
The code I used was
template.php:
function ThemeName_preprocess_page(&$vars) {
// if this is a panel page, add template suggestions
if($panel_page = page_manager_get_current_page()) {
// add a generic suggestion for all panel pages
$variables['theme_hook_suggestions'][] = 'page__panel';
// add the panel page machine name to the template suggestions
$variables['theme_hook_suggestions'][] = 'page__' . $panel_page['name'];
$object = $panel_page['contexts']['argument_entity_id:node_1'];
$result_array = get_object_vars($object);
$value = $result_array['restrictions']['type']['0'];
if($panel_page['name'] == 'node_view' AND $value == 'product' ) {
$vars['theme_hook_suggestions'][] = 'page__node_view_product';
}
if($panel_page['name'] == 'node_view' AND $value == 'artist' ) {
$vars['theme_hook_suggestions'][] = 'page__node_view_artist';
}
and I created a files under ThemeName/templates
page--node_view_artist.tpl.php and
page--node_view_product.tpl.php
I hope this helps someone, it took me a long time to figure it out!
I have a form in CakePHP with a few dozen fields in it. From all the examples I have seen, there is duplicate form code for an add view and an edit view.
Is there any tricks to keep the duplication out? What is best method in CakePHP for this?
What I do, is to put all form fields in an element, and then insert the element in the add.ctp and edit.ctp
Don't forget to add the hidden field with the id in the edit.ctp
This way all visible elements are in one file, easier to maintain.
View/MyModel/add.ctp
echo $this->Form->create('MyModel');
echo $this->element('my_form');
echo $this->Form->end();
View/MyModel/edit.ctp
echo $this->Form->create('MyModel');
echo $this->Form->input('id');
echo $this->element('my_form');
echo $this->Form->end();
View/Elements/my_form.ctp
// your form inputs
// whatever they are
You should NOT merge those views, because add/edit are different actions and deserve separate view files. As your application grows you will realize that its good to have separate views to reduce complexity of if else conditions.
If you still want to avoid the separate files, Use
function add() {
.....
$this->render('edit')
}
I've done this before, but reverted back to having separate views, mainly for my own sanity.
It's easy enough to do. The edit requires an input for the record id. This is usually hidden. Any default form values for the add form will have to be contained in conditionals so that the stored values are not overwritten with defaults when you are editing a record
On the controller side of things, you'll need a conditional statement to decide whether to act as an add or edit depending on whether the $this->data['MyModel']['id'] is set.
I think that covers it - if I think of anything else I'll add it in.
My work pattern tends to be to build the edit view, then copy and paste to create the basis for the add view.
this code will check if you have admin_form.ctp or form.ctp which will make it use the same code for add / edit
https://github.com/infinitas/infinitas/blob/dev/app_controller.php#L389
1.3 submits the forms automatically to where the are from so when you go to /edit/1 it will post to there, and /add will post to add.
that is all you need to do. if you have a edit that is vastly different to the add, then you just create the 2 files. when you want them the same, just make the one.
in your app controller
public function render($view = null, $layout = null) {
$viewPaths = App::path('View', $this->plugin);
$rootPath = $viewPaths[0] . DS . $this->viewPath . DS;
$requested = $rootPath . $view . '.ctp';
if (in_array($this->request->action, array('admin_edit', 'admin_add', 'edit', 'add'))) {
$viewPath = $rootPath . $this->request->action . '.ctp';
if (!file_exists($requested) && !file_exists($viewPath)) {
if (strpos($this->request->action, 'admin_') === false) {
$view = 'form';
} else {
$view = 'admin_form';
}
}
}
return parent::render($view, $layout);
}
and in your view you can always check whether its edit or add
if ($this->request->params['action'] == 'admin_edit') {
//do something
}
if ($this->request->params['action'] == 'admin_add') {
//do something
}
in edit.ctp
if($this->data[ModelName]['id']) {
$this->Form->input('id');
}
// create rest of the fields
in Controller::add()
$this->autoRender=false; // at the start of function
$this->render('edit.ctp'); // at the point where you actually want to render
no need to create add.ctp