Cannot render submit button - drupal-7

I've customized the edit profile view using template.php and user-profile-form.php
All shows up correctly but the Submit (and Delete) button..
I'm using Adaptive Theme and I've modified like this :
template.php
function adaptivetheme_theme(&$existing, $type, $theme, $path) {
return array(
'user_profile_form' => array(
'template' => 'templates/user-profile-form',
'render element' => 'form',
),
);
}
function adaptivetheme_preprocess_user_profile_form(&$vars) {
$vars['form']['account']['name']['#description'] = t('blabla');
$vars['form']['submit']['#value'] = t('Save profile');
$vars['form']['delete']['#value'] = t('Delete account');
$vars['account'] = drupal_render($vars['form']['account']);
$vars['theme_select'] = drupal_render($vars['form']['theme_select']);
$vars['picture'] = drupal_render($vars['form']['picture']);
$vars['signature_settings'] = drupal_render($vars['form']['signature_settings']);
$vars['contact'] = drupal_render($vars['form']['contact']);
$vars['timezone'] = drupal_render($vars['form']['timezone']);
$vars['submit'] = drupal_render($vars['form']['submit']);
$vars['delete'] = drupal_render($vars['form']['delete']);
}
then in the user-profile-form.tpl.php :
<div id="user-profile-form">
<?php echo $account; ?>
<?php echo $timezone; ?>
<?php echo $submit; ?>
<?php echo $delete; ?>
</div>
The edit form of the account shows correctly. I've tried adding/removing variables successfully (ie the $timezone) but the submit/delete are missing.
I don't know what's wrong..
I've tried to change the name of the variables 'submit' and 'delete' but still no button shows up. Of course I've cleared the cache every times needed (and not).
I have no JS hiding the buttons neither..
I render this form through a custom block in a Panel :
<?
module_load_include('inc', 'user', 'user.pages');
global $user;
print drupal_render(drupal_get_form('user_profile_form', $user));
?>
Maybe a problem with Panels ???
Any idea is appreciated :)
Thx for reading
Erwan

I forgot the "[action]".. :
$vars['submit'] = drupal_render($vars['form']['actions']['submit']);
$vars['cancel'] = drupal_render($vars['form']['actions']['cancel']);
And 'Delete' button didn't show up at first because it's called "cancel", and its #access param was sent to FALSE. Thx DPM ;)
Now, the problem is that when I trigger the submit button, the form is not sent, it justs reload he page. I will update if I manage to solve that too.

The page is only reloading because you forget to render the hidden form elements. To do so in your template preprocess you can use something like that :
function THEME_preprocess_user_profile_form(&$variables) {
$hidden = array();
foreach(element_children($variables['form']) as $key)
{
$type = $variables['form'][$key]['#type'];
if($type == "hidden" || $type == "token"){
$hidden[] = $variables['form'][$key];
}
}
$variables['hidden'] = $hidden;
//Dont forget to report your variables like you already did ...
}
Then when it s done render the $hidden variable in your template file
<?php print render($hidden);?>
And there you go !

Related

Create links to tagged list from an array of tags

I just followed a tag tutorial (https://book.cakephp.org/3.0/en/tutorials-and-examples/cms/tags-and-users.html) on the Red Velvet Cookbook. I was successful in adding tagging to my website. Though I feel it needs a couple more things. The comma separated list of tags is just that, a list. I would prefer a list of links. The links would obviously link to a list of posts that are tagged with the same tag. This list already exists thanks to the tutorial. The url looks like http://localhost:8765/story/tagged/cats/
I have fiddled with the html helper to try and create links but I am unsure how to do this with an array. Any leads or help would be greatly appreciated.
This is the line in my template that shows the list of tags
<p><b>Tags:</b> <?= h($story->tag_string) ?></p>
This is the function that gets the tag_string
class Story extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* #var array
*/
protected $_accessible = [
'*' => false,
'tag_string' => true
];
protected function _getTagString()
{
if (isset($this->_properties['tag_string'])) {
return $this->_properties['tag_string'];
}
if (empty($this->tags)) {
return '';
}
$tags = new Collection($this->tags);
$str = $tags->reduce(function ($string, $tag) {
return $string . $tag->name . ', ';
}, '');
return trim($str, ', ');
}
}
In your $story entity you have tags property, which you already used in computed field tag_string. To get separated tags, you need to iterate over it in a template, eg:
<ul>
<?php foreach($story->tags as $tag): ?>
<li><?= $tag->title ?></li>
<?php endforeach; ?>
</ul>
Assuming you have a method in your StoriesController named tagged, which accepts one parameter: tag title, you can construct link using HtmlHelper for each tag:
<ul>
<?php foreach($story->tags as $tag): ?>
<li>
<?= $this->Html->link($tag->title, [
"controller" => "Stories",
"action" => "tagged",
$tag->title
], [
"class" => "your-tag-link-class" //second array is for properties, in case you would like to add eg class for these links to style them using css.
]) ?>
</li>
<?php endforeach; ?>
</ul>

Drupal Ctools Modal Handling

I'm calling a function with URL http://mywebsite.org/param/ajax?ID=2 which will return a form in html for which i use the following functions in the corresponfin callback function in my custom module.
$form_state = array(
'ajax' => TRUE,
'title' => t("Edit : $ID"),
);
$output = ctools_modal_form_wrapper('form_fn', $form_state);
print ajax_render($output);
drupal_exit();
I use this output to create a form inside a twitter bootstrap modal. The problem is when I'm submitting I get an ajax output again which I'm not able to handle. I would like to show a success message or failure based on the form_submit function. Is there any way I can show the output using the modal or on the original page which called the modal.
call to ctools modal should look like this:
$output = ctools_modal_form_wrapper($form_name, $form_state);
if(!empty($form_state['executed'])) {
$output = array();
$output[] = ctools_modal_command_display(t('Successful message title'), 'Successful message body');
$output[] = ctools_ajax_command_reload();
}
print ajax_render($output);
exit;
Don't forget to check for $form_state['executed'].
Also if you use a ctools_ajax_command_reload() this will reload a content of a modal and should fix the problem with another ajax output.
Would upvote sirBlond but my reputation isn't up there yet. I used this in the case where my modal window was not closing in IE8 on successful form submission. I needed to add this right before my redirect.
$commands[] = ctools_ajax_command_reload()
$commands[] = ctools_ajax_command_redirect($path, 0, $options);
Was exactly what I needed. Thanks.

Navigation in CakePHP

I am new in CakePHP. This is the navigation part I am using in 1 of my CakePHP websites :
<?php
$list=array(
$this->Html->link('Home',array('controller'=>'pages','action'=>'index')),
$this->Html->link('About',array('controller'=>'pages','action'=>'about')),array(
$this->Html->tag('span',null,array('class'=>'top')),
$this->Html->tag('span',null,array('class'=>'bottom')),
$this->Html->link('Sub Menu 1',array('controller'=>'','action'=>'')),
$this->Html->link('Sub Menu 2',array('controller'=>'','action'=>'')),
$this->Html->link('Sub Menu 3',array('controller'=>'','action'=>'')),
),
$this->Html->link('Gallery',array('controller'=>'pages','action'=>'gallery')),array(
$this->Html->tag('span',null,array('class'=>'top')),
$this->Html->tag('span',null,array('class'=>'bottom')),
$this->Html->link('Sub Menu 1',array('controller'=>'','action'=>'')),
$this->Html->link('Sub Menu 2',array('controller'=>'','action'=>'')),
$this->Html->link('Sub Menu 3',array('controller'=>'','action'=>'')),
),
$this->Html->link('My Posts',array('controller'=>'pages','action'=>'myPosts/1')),
$this->Html->link('Blog',array('controller'=>'pages','action'=>'blog')),
$this->Html->link('Contact',array('controller'=>'pages','action'=>'contact')),
$this->Html->link('Logout',array('controller'=>'users','action'=>'logout'))
);
echo $this->Html->nestedList($list);
?>
What I want, "My Posts" and "Logout" menus will be shown if and only a user is logged in, otherwise not. How to do it ? And, do you have any better idea to make a navigation bar in CakePHP ?
Here span tags are used only for design issue.
1st Solution
You have to create a separate array for logged in user and this can be done via auth functions like
In Controller
$this->set('authUser', $this->Auth->user());
OR
$this->set( 'authUser', $this->Auth->loggedIn());
In View
If($authUser){
$list = array(/*without Post and Logout*/);
} else{
$list = array(/*same complete array*/);
}
2nd Solution
if ($this->Session->read('Auth.User')){
$list = array(/*without Post and Logout*/);
} else{
$list = array(/*same complete array*/);
}

Which Submit Button was Clicked in CakePHP?

Which Submit Button was Clicked in CakePHP?
** what is solution for 2 buttons on same form with only 1 action in cakephp? **
i have following code,1 form contain 2 buttons print & ship,if i click
print button,page is redirected on printorder page but problem is , if i click ship
button it is not working & page is redirected on printorder.
==========================================================================
<?php // in index.ctp file
echo $this->Form->create('User',array('action'=>'orders','type' =>'post'));
echo $this->Form->submit('Ship',array('name'=>'user','value'=>'Ship','id'=>'1'));
echo $this->Form->submit('Print',array('name'=>'user','value'=>'Print','id'=>'2'));
echo $this->Form->end();
?>
<?php // in UserController
public function orders()
{
if($this->params->data['form']['user'] = "Print" )
{
$this->redirect(array('action'=>'printorder'));
}
if($this->params->data['form']['user'] = "Ship")
{
$this->redirect(array('action'=>'shiporder'));
}
}
?>
It's because you have created one form and you are submitting 2 different values on the "USER" form.
so it will redirected because action of the form is common.
To avoid it.. Using of 2 different forms are the best way.
Another way is to use javascript but I suggest to use 2 different forms.
Typical solution to two submit buttons situation is to create a submit button (default action) and a regular button to handle another action. Than you can use the JavaScript to implement the behaviour of the second button, e.g. to set a value of a hidden field which contains the actual 'action' string:
echo $this->Form->create('User',array('action'=>'orders','type' =>'post'));
echo $this->Form->hidden('submit_action', array(id='submit_action')); ?>
echo $this->Form->submit('Ship',array('value'=>'Ship','id'=>'ship_button'));
echo $this->Form->button('Print',array('value'=>'Print','id'=>'print_button'));
echo $this->Form->end();
And the js:
<script>
var form = <get your form here>
var print_button = document.getElementById('print_button');
print_button.onclick = function() {
// Set value of print button to the #submit_action hidden field
document.getElementById('submit_action').value = print_button.value;
// Submit the form
form.submit();
}
</script>
I followed the top voted (not selected solution) from Two submit buttons in one form, which suggested applying different values to each submit button, and checking those on submit.
However, CakePHP didn't easily play with this technique, as despite setting 'value' key in the $options array of $this->Form->submit, no value was set on the generated element. So, I followed the suggestions from here, and used the 'name' value key.
view.ctp
$this->Form->submit('Submit 1', array('name' => 'action1'));
$this->Form->submit('Submit 2', array('name' => 'anotherAction'));
controller.php
if (array_key_exists('action1', $this->request->data)) {
/** setup any data you want to retain after redirect
* in Session, Model, or add to redirect URL below
*/
// redirect to another action
$this->redirect(array('action' => 'myOtherAction'));
} else if (array_key_exists('anotherAction', $this->request->data)) {
// do something with anotherAction submit
}

Populate Webform hidden field with title of referring node

Drupal 7
I'm having a similar problem to one that's been presented previously but so far I've not been able to make any of the suggestions work.
I have 'Product' pages of content type 'Software Products'. I want to place a link on the product pages pointing to a Webform 'Request Information' I want to populate a (hidden) field on the form with the product name which is also the title of the referring product page.
I have tried the following but this just results in the title of the form being shown - not the referring page.
<?php
/**
* Implementation of hook_form_alter().
*/
function AddNodeInfoToForm_form_alter(&$form, $form_state, $form_id) {
switch($form_id) {
case 'webform_client_form_10': // the id of the form
{$current_object = menu_get_object();
$product_title = $current_object->title;
$form['submitted']['product']['#default_value'] = $product_title; }
return $form;
}
}
I would appreciate any pointers - I'm new to Drupal
That's quite a messy way round of doing what you need to, you should just put the product nid in the URL as part of the query string in the link from your product page and then load it up from the webform.
In your node template/preprocess:
$webform_path = 'node/10'; // Or whatever the webform's nid is
$link = l('Request Information', $webform_path, array(
'query' => array(
'product_nid' => $product_node->nid
)
));
echo $link;
Then in your form alter:
function AddNodeInfoToForm_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'webform_client_form_10' && isset($_GET['product_nid']) && is_numeric($_GET['product_nid'])) {
$product_node = node_load($_GET['product_nid']);
if ($product_node) {
$product_title = $product_node->title;
$form['submitted']['product']['#default_value'] = $product_title;
}
}
}
Note that you don't return the form from the hook_form_alter function, the $form variable is passed in by reference so changes are stored that way.

Resources