Drupal clean urls on custom page GET request - php

I have a drupal page (content type page) that should accept GET values using clean urls.
I put the following code in the page body using the php code input format.
<?php
$uid = $_GET['uid'];
$user = user_load($uid);
print $user->name;
?>
Now the following link http://www.example.com/mypath/?uid=5 results in user 5's name being displayed. Great.
My question is: Can this be accomplished using clean urls such that going to http://www.example.com/mypath/5 has the same result? (Similar to using arguments in views)

You can do what you ask in the menu system using hook_menu in a module, but you can't create an alias where a part of it is a name. The whole alias is a name, so you can't extract info from it. Using hook_menu you can do this:
function my_module_menu() {
$items = array();
$items['path/%user'] = array(
'title' => 'Title',
'page callback' => 'callback',
'page arguments' => array(1),
'access callback' => '...',
'access arguments' => array(...),
);
return $items;
}
Then in your callback function you will have the value or argument 1 in the path which corresponds to the uid (in reality it will be a loaded user object because of %user).

Yes. Use arg() instead of $_GET
Keep in mind that arg() uses the $_GET['q'] by default and so effectively translates an alias before returning the result. Thus, the parameter you are trying to access may be in another position.
For example, the path myalias/5 might translate into taxonomy/term/5. In this case, arg(2) is 5.
For more information on the Drupal arg() function, see http://api.drupal.org/api/function/arg/6

If you use Views to assemble these pages, that part can be done easily for you by putting UID as an argument provided in the URL.

Related

Create new page in drupal

Iam new in drupal. I want to create a page and want to add data in page directly with out using backend content. I have created a page like this, page-test.tpl.php. How can I call this newly created page in browser?
First you have to create your own custom module.Let the module name be test.Create 2 files namely test.info and test.module.Declare a hook_menu function inside the test.module file.Since your module name is test your hook_menu will be named as test_menu.
Hook_menu enables modules to register paths in order to define how URL requests are handled. Paths may be registered for URL handling only, or they can register a link to be placed in a menu.
Test.module
function test_menu() {
$items = array();
$items['test'] = array(
'title' => 'My Page',
'description' => 'Study Hard',
'page callback' => 'test_simple', //Calls the function
'access arguments' => array('access content'),
);
return $items;
}
function test_simple() {
return array('#markup' => '<p>' . t('My Simple page') . '</p>');
}
Test.info
name = Test
description = Provides a sample page.
core = 7.x
After adding this try clearing the drupal cache and then navigate to /test.Try this link if you are a beginner.Try reading hook_theme which allows you to use your custom template.More about hook_theme here.Hope it might help u mate.. :)
Pages in drupal are stored in the database, not in actual code files like old websites used to. The template files (.tpl.php) are exactly that - templates. So for example, lets say you have blog content. You'd have to create a content type of 'Blog'. you could then create a template file (ex. node--blog.tpl.php) which would format the way the blog pages would look. But you would still need to add new pages through the drupal interface in order to add them to the site. You could always use an input type of PHP if you'd like to include php in the body of the page.
If you simply want to display a php file in your browser from your site, you can just upload a php file of your choice to your sites/default/files directory and access it directly via the file path.

How am I supposed to be using filter_xss? Even though I'm using it, coder gives me an issue

+269: [critical] Potential problem: drupal_set_message
http://api.drupal.org/api/function/drupal_set_message/() only accepts
filtered text, be sure all !placeholders for $variables in t
http://api.drupal.org/api/function/t/() are fully sanitized using
check_plain http://api.drupal.org/api/function/check_plain/(),
filter_xss http://api.drupal.org/api/function/filter_xss/() or
similar.
Which pertains to this code:
drupal_set_message(t('Batch complete! View/Download !results', array(
'!results' => filter_xss(l(t('simple results'), file_create_url($filename))),
)), 'info');
What's going wrong?
The method you're using is under the 'DO NOT DO THESE THINGS' portion of Dynamic or static links in translatable strings. You need to change it to one of the approved methods. For reference:
<?php
// DO NOT DO THESE THINGS
$BAD_EXTERNAL_LINK = t('Look at Drupal documentation at !handbook.', array('!handbook' => ''. t('the Drupal Handbooks') .''));
$ANOTHER_BAD_EXTERNAL_LINK = t('Look at Drupal documentation at the Drupal Handbooks.');
$BAD_INTERNAL_LINK = t('To get an overview of your administration options, go to !administer in the main menu.', array('!administer' => l(t('the Administer screen'), 'admin'));
// Do this instead.
$external_link = t('Look at Drupal documentation at the Drupal Handbooks.', array('#drupal-handbook' => 'http://drupal.org/handbooks'));
$internal_link = t('To get an overview of your administration options, go to the Administer screen in the main menu.', array('#administer-page' => url('admin')));
?>

How to show a forum on a custom path?

By default forums are enabled on forum on my site; I want them to move to share/forum. What are the different approaches I can take for it, and which one is best?
I tried doing this with the Pathauto module, but then the breadcrumbs are showing "Home >> forum" when I would expect it to show "Home >> share >> forum."
there are many ways,
1) either you can use pathauto(which u used but it uses default nid to track things so thats why in breadcrumbs it's showing like that)
2) use subdomain module and shift ur forum (if ur host is supporting that)
3) use a use separate forum module like phpbb or so and put it to ur desired path and using redirect.
whichever suits u..
If you want to show the forum pages only on share/forum, you can use the following code in a custom module.
function mymodule_menu_alter(&$items) {
if (isset($items['forum'])) {
$items['share/forum'] = $items['forum'];
unset($items['forum']);
}
if (isset($items['forum/%forum_forum'])) {
$items['share/forum/%forum_forum'] = $items['forum/%forum_forum'];
unset($items['forum/%forum_forum']);
}
}
If you want the forums to be visible in both the old, and the new path, you can use the following code.
function mymodule_menu() {
$items['share/forum'] = array(
'title' => 'Forums',
'page callback' => 'forum_page',
'access arguments' => array('access content'),
'file' => 'forum.pages.inc',
);
$items['share/forum/%forum_forum'] = array(
'title' => 'Forums',
'page callback' => 'forum_page',
'page arguments' => array(1),
'access arguments' => array('access content'),
'file' => 'forum.pages.inc',
);
return $items;
}
As for the breadcrumbs, you need to implement hook_menu_breadcrumb_alter(), considering what reported in the documentation page.
Implementations should take into account that menu_get_active_breadcrumb() subsequently performs the following adjustments to the active trail after this hook has been invoked:
The last link in $active_trail is removed, if its 'href' is identical to the 'href' of $item. This happens, because the breadcrumb normally does not contain a link to the current page.
The (second to) last link in $active_trail is removed, if the current $item is a MENU_DEFAULT_LOCAL_TASK. This happens in order to do not show a link to the current page, when being on the path for the default local task; e.g. when being on the path node/%/view, the breadcrumb should not contain a link to node/%.

Serve an HTML5 application as a Drupal Module

I manage a Drupal 7 based website for my College's Student's Association. Most of it is standard static pages. Each year we run a room ballot for people to choose their rooms and this process will need an application to display current room allocations (in real time) and wiki style information about what the different rooms are like.
I need to be able to serve up static pages of HTML, javascript and css; bypassing the theming module. I need the relative addressing in the html page which serves as the root of the application to work properly (e.g. "javascript/app.js" should pick up that file from within the module). I then need to serve up json data from php using all the drupal APIs for permissions and database access etc.
I have a fair bit of experience in HTML, Javascript etc. and some in PHP, but I'm fairly new to Drupal module development.
You should create a custom module as you suggest, and separately put your HTML5 application in a sub-folder of the module. When it's accessed it will use the same relative paths as you'd normally expect so javascript/app.js will work if the file exists in the path under your HTML5 app's folder.
For the JSON data your custom module will look something like this:
function mymodule_menu() {
$items['my/app/data'] = array(
'page callback' => 'mymodule_ajax_callback',
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
return $items;
}
function mymodule_ajax_callback() {
$type = $_POST['type'];
$nodes = db_query("SELECT nid, title FROM {node} WHERE type = :type", array(':type' => $type))->fetchAllKeyed();
drupal_json_output($nodes);
drupal_exit();
}
That code defines a menu path (using hook_menu()) at mp/app/data which uses mymodule_ajax_callback() as it's page callback.
mymodule_ajax_callback() simply grabs all nodes from the database that match the type parameter passed in the AJAX $_POST and outputs their id and title in a JSON string to the page (which will then be returned as your AJAX response when you request /my/app/path).
Hope that helps

Drupal 7 - Update node fields via ajax/frontpage

I have a few jQueryUI draggable objects that represent nodes generated on my front page in Drupal.
I want to grab when a user drops an element and save the x/y coordinates to the server so when the next user opens the page it will still be where it was last left at.
I created two integer fields, homex and homey, but I can't seem to figure out or find enough documentation to learn how to tell Drupal to update the values for a given node.
I'm fairly familiar with how to create modules in Drupal, and ajax in general - but combining the two in this case is perplexing me.
Can someone help me understand how to attach to Drupal so I can save the coordinates dynamically?
What would be preferable is if I could just write a simple handler module for Drupal that takes the x/y pair in a get/post request then updates them in the database and responds with a success/json. Really if it wasn't being done in Drupal this would be a fairly simple setup.
It seems all I had to do was create a hook_menu() and a hook_ajax_callback() (sorry couldn't find a link) in a module.
Here's what I ended up with (more less, leaving in three different return methods I was playing with):
<?php
function homepage_coords_menu(){
return array(//$items
'homepage_coords/%node/%/%' => array(
'page callback' => 'homepage_coords_ajax_callback',
'page arguments' => array(1,2,3),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
)
);
}
function homepage_coords_ajax_callback($node,$x=0,$y=0){
if(!is_numeric($x) || !is_numeric($y)){
ajax_deliver(json_encode(array(
'status'=>'fail'
)));
}
$node->field_homepagex = array('und'=>array(array('value'=>$x)));
$node->field_homepagey = array('und'=>array(array('value'=>$y)));
node_save($node);
ajax_deliver(json_encode(array(
'status'=>'win'
)));
}
?>
When you say that you are familiar with ajax, you mean jquery's ajax or Drupal 7 ajax framework. You can read more about the Drupal 7 Ajax here http://drupal.org/node/752056
I suppose homex is a hidden form element. Maybe you could hook_form_alter it, adding a #ajax attribute with an ajax callback triggered by a "change" event for example or any other Jquery event, and in that ajax callback, execute node_form_submit_build_node

Categories