Validate l() before moving to different location - drupal-7

This would be most simple functional question in Drupal 7.
Now below mentioned things are mandatory to do and cant change the functionality.
1) I have created a custom block in module.
2) I have included form like below:-
function usercontent_block_view($delta='')
{
$block = array();
switch($delta)
{
case 'user_front_page' :
$block['content'] = drupal_get_form('genre_all_login');
break;
}
return $block;
}
3) Now I have some other content in block but which put me in trouble is shown below:-
function genre_all_login($form) {
$form['new_user'] = array(
'#type' => 'item',
'#markup' => l('Add New User','http://www.google.com'),
);
return $form;
}
4) So it includes a link which will move me to Google once clicked.
5) Now I want to validate certain content inside the block before user navigates to Google.So if the condition doesn't match then I will stop the user by showing an error message and hence user cannot traverse to Google.
Please tell me if it is possible or should I use different technique to handle this scenario.

You can set the #markup like:
l('Add New User','http://www.google.com', array('attributes' => array('class' => 'validate_with_js') ))
And then you can write a JavaScript code to validate:
jQuery(document).ready(function() {
jQuery('.validate_with_js').click(function() {
// Your Validation Cdoe. You can use AJAX here if you need to validate through PHP.
});
});
If you want to validate after submission of the form then you can use #element_validate.

Related

Process post action with another action within controller

I have a terribly designed app where the index action displays a form in a JavaScript-based dialogue that's submitted to process action and then redirects to index (either on success or on error). The process action does not even have a view:
class UnicornsController
{
public function index($foo, $bar)
{
$this->set(
array(
'unicorn' => $this->Unicorn->findByFooAndBar($foo, $bar);
)
);
}
public function process()
{
$this->Unicorn->save($this->request->data);
$this->redirect(
array(
'action' => 'index',
$this->request->data['Unicorn']['foo'],
$this->request->data['Unicorn']['bar'],
)
);
}
}
I'm adding proper error reporting. I'm trying to change the this->redirect() part so $this->request->data is not lost and I have a chance to display it again in the form generated in index.ctp but I can't get it right: both $this->requestAction() and $this->index() try to render process.ctp anyway. Am I using them incorrectly or I'm missing the right approach?
If you want to run a different action, you can use Controller::setAction(), it changes the action parameter in the request, sets the template to render accordingly, and returns the possible return value of the invoked action.
public function process()
{
// ....
$this->setAction(
'index',
$this->request->data['Unicorn']['foo'],
$this->request->data['Unicorn']['bar']
);
}
See also
API > Controller::setAction()

how i can use onblur function in cakephp form?

I have written this validation rule in Model but want to validate form data without pressing submit button.
Code:
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
)
);
Do i need to put these rules in function to apply javascript?
without pressing button and for processing you can do this with java script, like
<script>
var input = document.getElementsByName('title')[0];
input.onfocus = function() {
(or do anything else what you need)
this.className = 'some_class_name';
}
input.onblur = function() {
(or do anything else what you need)
this.className = '';
}
</script>
Or with jQuery or any another library used for project

In Drupal 7 include a php file in block from a custom module

I am new to Drupal and PHP. I am trying to create a module that when activated creates a page, then creates a block, configures block to only show on the created page, and then display an included PHP file. The PHP file that is included is for accessing an off site database.
I have managed to code the module to create the page, and create the block. But the following is my issue.
In the hook_block_view, for content I have input a function name, below I defined the function to include testpage.inc. that page loads but the content does not load in the block's specified region. If I just input text for the content, it loads in the region no problem.
When I define the include as the output value, the block loads the file path, and the included file loads at the top of the page outside of the block.
The Included file is a PHP file that accesses an off site web-service to display a company's Inventory.
<?php
//////////////////////////////////////////////////////////////////
/** using hook_menu to create a page for the module - hook_menu**/
/////////////////////////////////////////////////////////////////
function sps_webconnect_menu() {
$items['products']=array(
'title' => 'Products',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'sps_webconnect_products',
'access callback' => TRUE,
);
return $items;
}
/** This function defines the page callback from above**/
function sps_webconnect_products() {
return "&nbsp";
}
////////////////////////////////////////////////////////////////
/** Creates and Defines Block - hook_block_info**/
////////////////////////////////////////////////////////////////
function sps_webconnect_block_info() {
$blocks['sps_webconnect_block'] = array(
'info' => t('Products Page - SPS Web Connect'),
'cache' => DRUPAL_NO_CACHE,
'status' => TRUE,
'region' => 'content',
'visibility' => BLOCK_VISIBILITY_LISTED,
'pages' => 'products',
);
return $blocks;
}
//////////////////////////////////////////////////////////////////////
/* This function defines the content of the block - hook_block_view */
/////////////////////////////////////////////////////////////////////
function sps_webconnect_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'sps_webconnect_block':
$block['subject'] = t('SPS Product Page');
$block['content'] = sps_webconnect_file();
break;
}
return $block;
}
function sps_webconnect_file(){
$output = '';
module_load_include('inc', 'sps_webconnect', 'testpage');
return $output;
}
You did not provide the content of the inc file but I am pretty sure it outputs (echo/print) data/html and it should not since the last function is supposed to return output.
So you could have a function inside your inc file that returns some html and then call it from your last function to populate $output.
Correct, The inc File did include an Echo. When I included a function the inc file and called that function for Output the inc file was loaded in the block. I found out that the reason for this is that Drupal uses Output Buffering, which compiles all of the markup before sending it to the browser. The File I was trying to include was not output buffer friendly.

Apply custom page template to content from Drupal 7 module

I am developing a Drupal module. Part of it opens a pop-up window, displays some elements in it, and uses JavaScript to transfer input back to the main page.
As this is a small window, I don't want it to display the full theme borders from the site's theme.
In Drupal 6 I was able to achieve this with the following:
function MyPopupPageHandler() {
#content = "Content...";
//Add additional content to #content;
print theme("page", #content);
}
However, Drupal 7 expects the second parameter of the theme() function to be an array, and none of the examples I've seen show how I set the main content of the page.
What I'd like is a custom page template in the module, that can be overridden by the site theme if it provides one.
I would like to know:
What elements do I need to put in the array I pass to the theme() function?
What should I call my template file?
How do I tell Drupal where to find my template, as it needs to be in the module by default?
Appreciate any help you can offer.
James
Try this
First of all create a menu in .module file
function MYMODULE_menu()
{
$items['Demo'] =array(
'title' => 'Demo Page',
'page callback' => 'demo_page', // call a function
'access arguments' => array('access content'),
);
return $items;
}
After you have create a function
function demo_page()
{
$select = db_select('node', 'n');
$select = $select->fields('n', array('id'))
->extend('PagerDefault');
$queried_nodes = $select->execute()
->fetchAllAssoc('id');
$pager = theme('pager');
return theme('demo_template', array('nodes' => $queried_nodes , 'pager' => $pager)); // call a theme or you have no pass any argument in theme to change a 'nodes'=> NULL or 'pager'=>NULL
}
after i have create a theme function
function MYMODULE_theme()
{
return array(
'demo_template' => array(
'template' => 'demo-page',//this is file name of template file
'variables' => array('nodes' => NULL,'pager' => NULL), //this is pass avarible of templates file
'path' => drupal_get_path('module', 'MYMODULE_NAME').'/template' // set a path of file
),
);
}
after you have create file name like demo-page.tpl.php in sites/all/modules/MYMODULENAME/template/
and clear caches
1) The second parameter of theme() function must be an associative array. Your function should look like :
function MYMODULE_custom_function() {
$content = "Some stuff";
return theme('MYMODULE_custom_output', array('content' => $content));
}
2) I guess you meant "Where should I call my template file?" In the hook_theme() function in your same .module file :
function MYMODULE_theme() {
return array(
'MYMODULE_custom_output' => array(
'variables' => array('content' => array()),
// You can also use template file here : 'template' => 'MYMODULE-template'
// OR use the following theme_function() if you don't want to create a new file
),
);
}
// If you don't use template file
function theme_MYMODULE_custom_output($variables) {
$output = // Arrange your html output here
return $output;
}
3) To tell where to find your custom template file if you decide to use one, you can read this : https://drupal.org/node/715160 and I hope it will help.
Please stay indulgent because I'm still new with Drupal and I did try to do my best here :o)

Cakephp load element with BBcode/shortcode

I am looking for input/help on how to do this. Might be some PHP/cake developers could provide some good solutions here. Cakephp 2.3 something :)
Problem; How to put shortcodes in wysiwyg editor (example: [slideshow=1]slideshow here[/slideshow]) and render an element (in this case, loading and displaying the slideshow with ID=1).
ShortcodeHelper.php
App::import('Helper', 'Html', 'Router');
class ShortcodeHelper extends AppHelper {
public $shortcodes = array(
'slideshow' => '/(\[slideshow=)(.+?)(\])(.+?)(\[\/slideshow\])/'
);
public $returncodes = array(
//'slideshow' => $this->render('/elements/slideshow', array('id'=>'\\2'))
'slideshow' => '<strong rel="\\2">\\4</strong>'
);
public function render($content, $render=null) {
$shortcodes = $this->shortcodes;
$returncodes = $this->returncodes;
if(isset($render)) {
$temp_shortcodes = array();
$temp_returncodes = array();
foreach ($render as $key => $value) {
$temp_shortcodes[$key] = $shortcodes[$value];
$temp_returncodes[$key] = $returncodes[$value];
}
$returncodes = $temp_returncodes;
$shortcodes = $temp_shortcodes;
}
$return = preg_replace($shortcodes, $returncodes, $content);
return $this->output($return);
}
}
view.ctp (call render function from helper, and run the page-content trough it):
<?php echo $this->Shortcode->render($page['Page']['body']); ?>
Thanks. You are awesome!! :)
-Tom
You need to turn the short code string into a method call, parse it.
Your helper will need to be able to detect them and then break them up. Your code needs to be mapped somehow to a callback.
// [slideshow=1]slideshow here[/slideshow]
$this->requestAction(array('controller' => 'slideshows', 'action' => 'view', $id);
For example.
I think the best way here would be to just always map the first arg, the "function call" to an element instead and pass all other args to the element. This way you can do there whatever you want and request the data or just simply display HTML only.
I would put the mapping of short codes into something like Configure::write('ShortCodes', $shortCodeArray); this way plugins could even register their callback mapping by simply adding them to that array.
array(
'slideshow' => array('controller' => 'slideshows', 'action' => 'view')
);
You'll have to merge that with args from the parsed short code.
Why requestAction()? You should not violate the MVC pattern, for this reason you'll have to request the data via requestAction().

Resources