different form override for node/add and node/edit pages? - drupal-7

Hia
I am using the following in a custom module to customise the form on the node/add/baby page for my BABY content type, however i want to make some slight modifications to this page when its the node/nid/edit page for the baby content type. is this possible?
<code>
<?php
function concept_theme() {
return array(
'baby_node_form' => array(
'arguments' => array(
'form' => NULL,
),
'template' => 'templates/baby-node-form',
'render element' => 'form',
),
);
}
?>
</code>
thank u

In template.php
function my_theme_name_theme($existing, $type, $theme, $path)
$hooks['baby_node_form']=array(
'render element'=>'form',
'template' =>'templates/node--baby-edit',
);
return $hooks;
}
In node--baby-edit.tpl.php
Hide fields like this:
<?php hide($form['title']); ?>
Manipulate fields like this:
<?php print render($form['field_image']); ?>
Use this to print all/rest of content:
<?php print drupal_render_children($form); ?>
Clear cache.

You can alter the node creation form by implementing this hook,
something similar to this:
hook_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'node_baby_form') {
//do modification to form array $form
}
}
Or if your node is defined by hook_node_info (which I believe is not the case), just changing the elements in hook_form()

Related

Error in writing a function in Cakephp

I'm working with cakephp for project development. I wrote a function to get data from the database and do a summation.I have created a table called user with height and weight in it. I want to retrieve that data,add them, and to return that. Here is the function I wrote.
public function calculate()
{
$Height=$this->set('user',$this->User->find('first'));
$Weight=$this->set('user1',$this->User->find('first'));
$sum=($Height+$Weight);
$this->set('SUM',$sum);
}
In the view I wrote the following code.
<div class="page-content">
<?php echo $user['User']['weight']; ?>
<?php echo $user1['User']['height']; ?>
<?php echo $SUM ?>
</div
Height and weight values are getting displayed. But the sum value is displayed as zero. Please help me to fix this.
I found a better way to do the above calculation. I used the following code in the controller.
public function calculate($id=null)
{
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$this->set('user1',$this->User->find('first', array('fields' => ('height'),'conditions' => array('User.id' =>$id))));
$this->set('user',$this->User->find('first', array('fields' => ('weight'),'conditions' => array('User.id' =>$id))));
}
My view looks similar to the one mentioned above. This works fine with me...
First you need get the field from find. (You can use find condition to filter de user id for example).
$user = $this->User->find('first', array(
'conditions' => array('User.id' => $id),
'fields' => array('User.height', 'User.weight') // fields from your DB
));
So, you can do a pr($user) to see the data and know what use.
After do the sum and set to the view. Cakephp Set

cakephp passedArgs empty

I have a simple form in a a view and I am trying to access the $this=>passedArgs but it is coming back empty.
I am actualy trying to use the cakeDC search plugin which uses the $this=>passedArgs. It must be something simple I have not done to get the results from the form submit.
find view
<?php
echo $this->Form->create('Member', array(
'url' => array_merge(array('action' => 'find'), $this->params['pass'])
));
echo $this->Form->input('name', array('div' => false));
echo $this->Form->submit(__('Search'), array('div' => false));
echo $this->Form->end();
?>
Controller
public function find() {
debug($this->passedArgs);
exit;
}
I have tried $this->request->params
array(
'plugin' => null,
'controller' => 'members',
'action' => 'find',
'named' => array(),
'pass' => array(),
'isAjax' => false
)
I have add method get to the form.
This question has been asked before but their solution of having lower cases in the public $uses = array('order', 'product'); when it should be public $uses = array('Order', 'Product'); did not work.
Cakephp version 2.3.5
Thanks for any help
Update:
I have set my form to method get and this is the url:
http://localhost/loyalty/members/find?name=searchtext
I have removed the plugin and I still do not get anything $this->passedArgs, but I now get data for $this->request->data['name']. Once I put public $components = array('Search.Prg'); I get noting again for $this->request->data['name'].
I have tried again $this->Prg->parsedParams() with the Search plugin and I just get array()
The documentation is pretty clear on that.
You cannot just debug something that has not been set yet.
So including the plugin itself (and its component) is not enough.
From the readme/documenation:
public function find() {
$this->Prg->commonProcess();
$this->paginate['conditions'] = $this->ModelName->parseCriteria($this->passedArgs);
$this->set('...', $this->paginate());
}
Note the commonProcess() call which then only makes passedArgs contain what you need.

Multiple form with same model name on single page cakephp

I have two form on a single page: login form and register form. When I submit the register form, it validates both: form fields that are in login and registeration. How can I handle it if both form have the same model (user model)
Register form
<?php echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'add'))); ?>
<?php echo $this->Form->input('username', array('label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('email', array('label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('password', array('label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('confirm_password', array('type' => 'password', 'label' => false, 'div' => false, 'class' => 'reg_input'));?>
<?php echo $this->Form->submit(__('Submit', true), array ('class' => 'reg_button', 'div' => false));
echo $this->Form->end();?>
and Login form is below
<?php echo $this->Form->create('User', array('controller' => 'users', 'action' => 'login'))?>
<?php echo $this->Form->input('User.username',array('label'=>false,'div'=>false, 'class' => 'reg_input'));?>
<?php echo $this->Form->input('User.password',array('label'=>false,'div'=>false, 'class' => 'reg_input'));?>
<?php echo $this->Form->submit(__('Log in', true), array ('class' => 'reg_button', 'div' => false)); ?>
<?php echo $this->Form->end();?>
When I submit registration form it validates both forms, I want to validate only the registration form.
How can I handle that?
I've come up with a "solution" (I find the approach dirty, but it works) for a different question (very similar to this). That other question worked with elements and views, though. I'll post the entire solution here to see if it helps someone (though I rather someone else comes with a different approach).
So, first: change the creation names for the two forms.
//for the registration
<?php echo $this->Form->create('Registration',
array('url' => array('controller' => 'users', 'action' => 'add'))); ?>
//for the login
<?php echo $this->Form->create('Login',
array('controller' => 'users', 'action' => 'login'))?>
The forms should work, look and post to the same actions, so no harm done.
Second step: I don't have your action code, so I'm going to explain what needs to be done in general
public function login() {
if ($this->request->is('post')) {
//we need to change the request->data indexes to make everything work
if (isset($this->request->data['Login'] /*that's the name we gave to the form*/)) {
$this->request->data['User'] = $this->request->data['Login'];
unset($this->request->data['Login']); //clean everything up so all work as it is working now
$this->set('formName', 'Login'); //we need to pass a reference to the view for validation display
} //if there's no 'Login' index, we can assume the request came the normal way
//your code that should work normally
}
}
Same thing for the registration (only need to change 'Login' to 'Registration').
Now, the actions should behave normally, since it has no idea we changed the form names on the view (we made sure of that changing the indexes in the action). But, if there are validation errors, the view will check for them in
$this->validationErrors['Model_with_errors']
And that 'Model_with_errors' (in this case 'User') won't be displayed in the respective forms because we've changed the names. So we need to also tweak the view. Oh! I'm assuming these both forms are in a view called index.ctp, for example, but if they are on separate files (if you're using an element or similar) I recommend add the lines of code for all the files
//preferably in the first line of the view/element (index.ctp in this example)
if (!empty($this->validationErrors['User']) && isset($formName)) {
$this->validationErrors[$formName] = $this->validationErrors['User'];
}
With that, we copy the model validation of the User to the fake-named form, and only that one. Note that if you have a third form in that view for the same model, and you use the typical $this->form->create('User'), then the validation errors will show for that one too unless you change the form name for that third one.
Doing that should work and only validate the form with the correct name.
I find this a messy approach because it involves controller-view changes. I think everything should be done by the controller, and the view shouldn't even blink about validation issues... The problem with that is that the render function of Controller.php needs to be replaced... It can be done in the AppController, but for every updgrade of Cakephp, you'll have to be careful of copying the new render function of Controller.php to the one replacing it in AppController. The advantage of that approach, though, is that the "feature" would be available for every form without having to worry about changing the views.
Well, it's just not that maintainable anyway, so better to leave it alone if it's just for this one case... If anyone is interested on how to handle this just in the controller side, though, comment and I'll post it.
You can duplicate your model and change his name and define $useTable as the same table name.
Example :
class Registration extends AppModel {
public $useTable = 'users';
You define the action in form->create like Nunser for your login form
<?php
echo $this->Form->create('User',array(
'url' => array(
'controller' => 'Users',
'action' => 'login',
'user' => true
),
'inputDefaults' => array(
'div' => false,
'label' => false
),
'novalidate'=>true,
));
?>
and your registration form
<?php
echo $this->Form->create('Registration',array(
'url' => array(
'controller' => 'Users',
'action' => 'validation_registration',
'user' => false
),
'inputDefaults' => array(
'div' => false,
'label' => false
),
'novalidate'=>true,
));
?>
In your controller define a method for registration validation and the most important define the render
public function validation_registration(){
$this->loadModel('Registration');
if($this->request->is('post')){
if($this->Registration->save($this->request->data)){
--- code ---
}else{
--- code ---
}
}
$this->render('user_login');
}
Sorry for my english ! Have a nice day ! :D
The create method on your login form is missing the 'url' key for creating the action attribute. I tried to re-create this once I fixed this and could not. Maybe that will fix it?

CakePHP: creating a dropdown on homepge through an element problem

I try to create a search function on my homepage where the user can limit the search results by country.
It all works in my posts/index controller whereby the country list is automatically retrieved by a find('list).
However, on the homepage, the country dropdown remains empty. Below some code:
I try to retrieve the dropdown by using requestAction (please omit 'requestAction is slow from the comments, thanks)
homesearch.ctp ELEMENT:
<?php $this->requestAction('countries/getCountries');?>
<?php
echo $this->Form->create('Post', array(
'url' => array_merge(array('controller' => 'posts','action' => 'index'), $this->params['pass'])
));
echo $this->Form->input('title', array('div' => false, 'empty' => true, 'label' => false));
echo $this->Form->input('country_id');
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?>
getCountries function in countries controller:
function getCountries(){
$countries = $this->Country->find('list');
$this->set(compact('countries'));
}
Before diving into alternatives (loadmodule('Country') in PagesController etc), I think I am doing something wrong, there is no data flowing back from the requestAction function as debug taught me.
How do you guys wash this cow? Thanks!
... (please omit 'requestAction is slow from the comments, thanks)
For improved performance, replace:
<?php $this->requestAction('countries/getCountries');?>
with:
<?php $this->viewVars['countries'] = ClassRegistry::init('Country')->find('list'); ?>
This approach doesn't generate a second request.
function getCountries(){
$countries = $this->Country->find('list');
if (!empty($this->params['requested'])) {
return $countries;
} else {
$this->set(compact('countries'));
}
}
and in the element: <?php $countries = $this->requestAction('countries/getCountries');?>
man, it's right in the book: http://book.cakephp.org/view/991/requestAction

Append Textarea with Cake PHP using Ajax

Quick questions
Is there Way to Append a textarea using Cakephp
view code:
<?php echo $ajax->link(
$qnote['Qnote']['subject'],
array(
'controller' => 'qnotes',
'action' => 'view', $qnote['Qnote']['id']
),
array( 'update' => 'Textarea_id')
);
?>
controller Code:
function view($id = null) {
$this->Qnote->id = $id;
$this->set('qnote', $this->Qnote->read());
}
the above code Pulls the information But Replaces the entire Text in the textarea.
Is there a way I can just append the textarea with out removing the existing text in the textarea
if possible can somebody point me in the right direction please.
You can try saving the result of your AJAX request to a hidden field and then having it execute and on page javascript function to simply slap the values from the hidden field to the visible text area.
The AJAX helper lets your specify callback functions, so something like this should work:
<?php echo $ajax->link(
$qnote['Qnote']['subject'],
array(
'controller' => 'qnotes',
'action' => 'view', $qnote['Qnote']['id']
),
array( 'update' => 'Textarea_id_hidden', "complete" => "concat_fields()" )
);
?>
and then the JavaScript in the View
<script type="text/javascript">
function concat_fields() {
$('#Textarea_id').val( $('#Textarea_id').val() . $('#Textarea_id_hidden').val() );
}
</script>
Note: My JavaScript example above assumes you're using JQuery, changes will need to be made if you're not.

Resources