Drupal 7 - Update node fields via ajax/frontpage - php

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

Related

custom drupal 6 is not showing on the page

I recently started working for this company and I am trying to modify the php code that was already created to display blocks on a page. I am using my localhost and wamp to test and develop my code. the first block that I created worked as the only thing that I did was to copy and modify the title of the code but now that I am trying to do the same for second and for some reason is not working. on my drupal 6 localhost block page I could see the block that I created and the region assign to it but is still not visible in the page. What am I missing?
Below is the code that I am using. Any help will be appreciated.
on the Page specific visibility settings this is the php code :
<p><?php return _admin_block_visibility('breeding_race_results'); ?></p>
$blocks['race_results'] = array('info' => t('Race Results'),
'status' => 1,
'region' => 'middle',
'visibility' => 2,
'weight' => -100,
'pages' => '<?php return _admin_block_visibility(\'race_results\'); ?>',
);
case 'race_results':
$block['subject'] = t('Race Results');
$block['content'] = admin_race_results_block_content();
break;
Generally, in Drupal, you don't need to write a code to have things displayed. Use Drupal core blocks management (Structure > Blocks) or use Context module to tell the system where you want certain elements like blocks to appear (what page or what type of pages etc.). Also, use Views module to create a dynamic lists of things to be displayed anywhere in the system.
Also, if a block is not showing where it suppose to, add there some text text and check if that will work. You will want to figure out first if the problem is with the code in the block (that makes the results invisible) or the problem is that the block is not rendering at all.

How to modify data for a SugarCRM SubPanel using PHP?

Is there anyway to modify the content shown in a SugarCRM Subpanel without relying on Action Hooks?
Right now to edit content for a Subpanel field I have to use the hooks like this...
$hook_array['process_record']
And in the Class method that I assign the Hook to call I can then change a field in the Subpanel like this...
$bean->name = '<a href="/index.php?action=ajaxui#ajaxUILoc=index.php%3Fmodule%3Dproje_Web_Project_Tasks%26action%3DDetailView%26record%3D'
.$bean->id.'" rel="popover" data-content="'
.$bean->description.'" data-original-title="">'.$bean->name.'</a>';
The main and major problem we have with this method is it works great until you do either of these actions....
Add an item using the Quick Create form
Change a page using the Subpanel paging buttons
In either case, it reloads the Subpanel data without running this hook code on the data, so the result is pretty major as the Subpanel fields that you have edited are no longer edited and show up as normal.
Here is a basic example...this shows 2-3 fields that have been edited using the Hook method above...
Now after paging or quick-creating a new record in the Subpanel, it reloads the Subpanel data and does not apply the Hooked code so you can see the result looks like this...
I know that ListView has a much more reliable and flexible method for editing it's content using the get_list_view_data() method I am able to apply the same edits and have them work all the time!
So I am hoping there is a similar method to edit Subpanel data and have it always apply the edits to that data? From what I have seen in my research so far, the only solution that will work as expected all the time, is to make a completely new Custom Field Type!
I am really hoping that is not the ONLY way as that is a major pain to do that for each type of field that I need to edit in the Subpanels and just doesn't feel right when there are easy ways to edit everything else except SubPanel data.
Does anyone have any ideas, suggestions, tips, help, please do share with me on this matter as it is the main problem I have had since I started developing with SugarCRM in the past few months?
You can change the data by writing a custom query to get data for your subpanel.
So inside your bean (this case Contacts) do a functions:
function get_project_list() {
$query = "SELECT project, info, matching, column, names FROM projects WHERE contact_id = '" . $this->id . "' AND deleted = 0"
return $query;
}
and then in subpanel definition set the data source like this:
$layout_defs["Contacts"]["subpanel_setup"]['projects'] = array(
'order' => 10,
'sort_order' => 'desc',
'sort_by' => 'name',
'title_key' => 'LBL_PROJECTS_TITLE',
'subpanel_name' => 'contact_projects',
'module'=>'projects',
'get_subpanel_data' => 'function:get_project_list',
'function_parameters'=>array('type'=>'urgent'), // this is optional if you decide to sent parameters to the function if do this dont forget to define your function with function get_project_list($params)
'top_buttons' => array (... buttons that you need go here..),
);
Since sql is quite powerful you can modify your subpanel data any way you like, well more or less :)

Symfony 2, how to redirect form response to a new window

I have a form that generates a pdf document that will be served to the user when submitted. I would like to have the pdf document open in a new window. How would I do that using Symfony forms? I've been doing some Google searching but have not been able to find any ready answers to how to do this.
I found the answer to my question and added it as reference for other Symfony developers. See my response below to learn how to use it in your project.
To get your form to redirect to a new window is insanely simple, once you know how. When you create your form ( if you are using form classes, which you should ), your form creation will look something like this:
$form = $this->createForm(new MyFormName(), $myObject);
To add the ability for your form to redirect, you need to add the third argument to your constructor, like this:
$form = $this->createForm(
new MyFormName(),
$myObject,
array(
'attr' => array(
'target' => '_blank'
)
)
);
Now, when your form is submitted, it opens the results in a new window.
I hope this information helps other developers from hitting their heads on their keyboards like I was doing.

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 - Automate a Content Form Submission

I would like to programatically (using php) fill out an existing drupal form to create a content type that is included in a contributed module.
Details: The module is SimpleFeed and the content type is Feed. I would like to call the module's functions to accomplish this. The method I am interested in is hook_insert which appears to require vid and nid which I am unsure what these are.
Any help is appreciated.
can you provide a bit more information (which modules?). generally, i'd probably suggest calling the modules functions to create the content type, instead of trying to pass it through a form programatically. this way you don't have to worry about implementation, and can trust that if the module works, it'll work for your script too :)
of course this does tie your module to theirs, so any changes in their functions could affect yours. (but then again, you run that risk if they update their database structure too)
ex.
// your file.php
function mymodule_do_stuff() {
cck_create_field('something'); // as an example, i doubt this
// is a real CCK function :)
}
edit: vid and nid are node ID's, vid is the revision id, and nid is the primary key of a particular node. because this is an actual node, you may have to do two operations.
programatically create a node
you'll have to reference the database for all the exact fields (tables node and node_revisions), but this should get you a basic working node:
$node = (object) array(
'nid' => '', // empty nid will force a new node to be created
'vid' => '',
'type' => 'simplefeed'. // or whatever this node is actually called
'title' => 'title of node',
'uid' => 1, // your user id
'status' => 1, // make it active
'body' => 'actual content',
'format' => 1,
// these next 3 fields are the simplefeed ones
'url' => 'simplefeed url',
'expires' => 'whatever value',
'refresh' => 'ditto',
);
node_save($node);
now i think it should automatically call simplefeed's hook_insert() at this point. if not, then go on to 2. but i'd check to see if it worked out already.
call it yourself!
simplefeed_insert($node);
edit2: drupal_execute() isn't a bad idea either, as you can get back some validation, but this way you don't have to deal with the forms API if you're not comfortable with it. i'm pretty sure node_save() invokes all hooks anyhow, so you should really only have to do step 1 under this method.
The drupal api provides drupal_execute() to do exactly this. I would suggest you avoid calling the functions directly to create the node (unless there is a performance reason). By using drupal_execute() all the proper hooks in other modules will be called and your code is far more likely to continue to work through future versions of drupal.
Note that a classic bug in using this method is not first calling something like
module_load_include('inc', 'node', 'node.pages')
which will load the code for your node creation form.
Calling node_save directly is generally considered deprecated and could leave you with broken code in future versions of drupal.
There is a nice example at this lullabot post

Categories