Let's assume that I have commerce website on Drupal. There is Drupal Commerce module. I want use api to retrieve complete orders from my store. Is it possible? I can't find any information how this requests looks like (what is response format etc.). Also I don't need php examples , maybe is there Rest API?
I think there are few ways to do it, from simple to (a bit) complex.
Since you mention retrieving data only, the simple way is to create a new Views listing out all orders, you can configure this views to render output as XML or as JSON. Module used can be Views data export, or Views datasource.
The second solution is to create a custom module which use hook_menu to create your custom endpoint URL, which calls a callback function where you can define the output you want
function YOURMODULE_menu() {
$items = array();
$items['api/order'] = array(
'title' => 'API Orders',
'page callback' => 'YOURMODULE_api_orders_callback',
'access arguments' => array('access content')
);
$items['api/order/%'] = array(
'title' => 'API Orders',
'page callback' => 'YOURMODULE_api_orders_callback',
'page arguments' => array(2),
'access arguments' => array('access content')
);
return $items;
}
function YOURMODULE_api_orders_callback($order_id = NULL) {
$order_ids = array();
if(!is_null($order)) {
array_push($order_ids, $order_id);
}
// Load all orders or only 1 order
// We can use commerce_order_load_multiple()
// or use EntityFieldQuery() if need a complex logic filtering orders
$orders = commerce_order_load_multiple($order_ids);
drupal_json_output($orders);
}
The last solution is to use Services modules, create a custom module and create your custom service where you can define your custom resource - in this case - commerce_order.
A tutorial to create custom services can be found here 👉https://valuebound.com/resources/blog/how-to-create-custom-web-services-for-drupal-7-0
Related
I am trying to write my first Drupal custom module (using drupal 6). I got the basic module working but I would like to add another page to the project. For example, right now my module's path looks like this: www.mysite.com/my_custom_module. I would like to add another page like this: www.mysite.com/my_custom_module/my_sub_page. I've tried adding a new .module and .info file but this does not work. I've also tried adding new items to the menu hook, like this:
my_custom_module.module:
function my_custom_module_menu(){
$items = array();
$items['my_custom_module'] = array(
'title' => "My Custom Module",
'page callback' => "my_custom_module_info",
'access callback' => true,
'type' => MENU_NORMAL_ITEM,
);
$items['my_custom_module/my_sub_page'] = array(
'title' => "My Sub Page",
'page callback' => "my_custom_module_sub_page_info",
'access callback' => true,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function my_custom_module_info(){
$result = 'My Page URL was hit';
return $result;
}
function my_custom_module_sub_page_info(){
$result = 'My Sub Page URL was hit';
return $result;
}
In this example, if I go to .../my_custom_module it works fine. But, if I go to .../my_custom_module/my_sub_page, it still load, and displays my_custom_module. When I debug with a break point in each function, only my_custom_module_info is hit. Not the sub page. What am I doing wrong? I this even the correct way to create multi pages in a module? Just an FYI, each of these pages will have different audiences. The first page is to allow a user to submit some form data. The second is to allow an elevated user view the data.
Thanks
jason
Let's say I have custom module with Drupal.
I want to let user to type what skill their have in first form and after that show their skill as a title in second form and type percentage of their skill in textfield.
function skillbar_form($form, &$form_state) {
$form['html5'] = array(
'#type' => 'textfield',
'#title' => t('HTML5'),
'#default_value' => variable_get('html5'),
'#description' => t('Enter a percent of your HTML5 skill'),
);
return(system_settings_form($form));
}
Multistep Form is you friend, if you are building your form using the Form API.
Otherwise there are various modules which could help you in building forms with more than one step. A couple of them being:
Multi-step forms
Multi-Step Registration
Note: Multistep forms generally collect all the data from various steps. The data is mostly submitted at the final step (which is also a best practice).
You can pass the data from one form to another through URL in the submit form of first form.
function skillbar_form_submit($form, &$form_state){
$data_a = $form_state['values']['html5'];
$form_state['redirect'] = array('url_page2', $data_a);
}
In the other form just retrieve the data as by passing the $data_a as argument. So $var will have the value as 'html5'.
example_form($form ,&$form_state, $var){
...
}
And for the URL you will have to send it in items array
$items['url_page2/%'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('page2_form', 1),
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
I'm not really sure what direction to go in.
I have a box on multiple pages that displays the status of various items. I want it easily update-able and would prefer it to be updated via a module. I'm not sure what direction to go in and just need a gentle push.
I have created a table in the drupal sql db but not sure how I would go about creating an admin tool in the drupal control panel to make changes to it.
Does anyone have any ideas of how I should go about this?
p.s. I'm using drupal 7
Custom admin pages can be defined as follows:
function mymodule_menu() {
$items['admin/def'] = array(
'page callback' => 'mymodule_abc_view',
'page arguments' => array(1, 'foo'),
'access arguments' => array('administer nodes'),
'title' => 'Foo settings',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
Where mymodule is the name of your module and mymodule_abc_view is the function that returns the markup for your admin page
I'm basically creating my own interface for choosing among different product options, in Drupal 7, using Ubercart.
I want to manually create an html form where I will be submitting post variables which represent the details of the product(s).
Is this something possible to do? Is there any documentation on this? I hope it's simple enough.
Create a custom module ( lets call it mymodule )
register a valid url where your html form can be accessed use hook_menu
$items['mymodule/submit'] = array(
'page callback' => 'mymodule_submit',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
Now create the function mymodule_submit() in your module and use $_POST variable to check for values and act accordingly.
One of our website requirements is to have a content type that let's you decide on the total amount of content types on the fly.
For instance, if I specify a number 10, then it ought to generate the content types consecutively, one of type 'textarea' and another of type 'radio' are created 10 times.
essentially to break it programmatically, it will create:
<?php
for(i=0;i<10;i++)
{
echo "<input type = 'textarea'></input>";
echo "<select><option>1</option><option>2</option></select>";
}
?>
This is pretty straightforward if I was dabbling with simple PHP files, but with Drupal 7's content types (CCK), it is posing a bigger challenge than what it ought to be IMHO. I have tried exploring modules that let you create content types on the fly and considered creating a custom content type programmatically which seems like another challenge altogether.
I am curious if anybody has an alternative for this and has dabbled with this challenge before. Your answers are most appreciated.
Thanks guys
To create content dynamic types in drupal 7 you will need to follow the below process:
Updated *
1) Create a menu path using hook_menu() which uses drupal_get_form(). This will allow you to gather all data for your users input for the dynamic content creation.
Example:
$items['newpost'] = array(
'title' => 'Create Post',
'description' => 'The main noticeboard',
'page callback' => 'drupal_get_form',
'page arguments' => array('customvishal_create_content'),
'access callback' => TRUE,
);
return $items;
2) Then use:
function customvishal_create_content($form, &$form_submit) // To create your form on that page
function customvishal_create_content_validate($form, &$form_state) // for any kind of validation
function customvishal_create_content_submit($form, &$form_state)
In this function you can submit the values into your new content type.
Here is where you will call the below functions.
3) Create an array which will hold the meta data about your content type.
// Define the node type.
$mystuff = array(
'type' => 'mystuff',
'name' => $t('my new Stuff'),
'base' => 'node_content',
'description' => $t('This is an example node type.'),
'body_label' => $t('Content')
);
// Set defaults.
$content_type = node_type_set_defaults($mystuff);
4) use node_type_save() to save/declare your content type.
node_type_save($content_type);
5) Create fields and then attach to your content type.
foreach (_mystuff_installed_fields() as $field) {
field_create_field($field);
}
// Create instances of fields.
foreach (_mystuff_installed_instances() as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = $mystuff['type'];
field_create_instance($instance);
}