Drupal create form for admins to change specific content - drupal

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

Related

How does Drupal commerce requests looks like?

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

Adding a tab to the node edit form in Drupal

I have added a tab to the node edit form in Drupal 7 like this (code snippet from hook_menu implementation, irrelevant lines removed):
'node/%/products' => array(
'title' => t('Products'),
'page callback' => 'some_function',
'page arguments' => array(
1
),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK
)
The tab shows up and works, however, the page is shown in the site's default theme, not in the admin theme. Also the other tabs are missing from the page that is displayed.
I tried fixing this by including this inside an implementation of hook_admin_paths, but it didn't make a difference:
return array(
'node/%/products' => TRUE,
);
How can I enforce my page to show in the admin theme and display the other tabs for the node edit form (such as "Edit", "Revisions" etc.) ?
EDIT: The box Use the administration theme when editing or creating content at admin/appearance is ticked, and system-defined pages such as node/%/edit display in the admin theme, but my new page does not.
I found out what I was doing wrong. In hook_menu % is used to denote an argument; in hook_admin_paths, these have to be replaced by asterisks. The following change to my implementation of hook_admin_paths solved it:
return array(
'node/*/products' => TRUE,
);
On /admin/appearance page, at the bottom of the page where you set the administration menu, check the value of the tickbox "Use the administration theme when editing or creating content".
I had the same problem and I solved with the following hook_menu :
<?php
function <mymodulename>_menu() {
$items = array();
$output['node/%node/mypath'] = array(
'title' => t('Title'),
'type' => MENU_LOCAL_TASK,
'page arguments' => array('node', 1),
'page callback' => 'callback_function',
'theme callback' => 'variable_get',
'theme arguments' => array('admin_theme'),
)
}
function callback_function() {
return 'My New Page.';
}
I think you need to have %node inside the path because that's the right way to "auto load" the node (Drupal take care of this) and pass it as an argument to callback_function where you can use the loaded node.

Ubercart checkout using a manual html form

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.

How to hook_load a CCK custom content type in a MENU_CALLBACK drupal

This is for the DRUPAL ninjas:
I am creating a menu_hook for my custom module called testmodule;
I want to call a MENU_CALLBACK and return a loaded CCK object of a custom content type I created called "VideoNodes"
example:
$items['save_video_data/%my_custom_cck_type/%'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'save_data_to_db',
'access arguments' => array('Save Data to my_custom_cck_type'),
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'title' => 'Save a Data!'
);
Now, with the above code, my understanding is that I am telling drupal to load my_custom_cck_type and use the array(2) position (my argument) as the id field of the node to find it in the database.
I also understand that, I am going to need to create a my_custom_cck_type_load() function.
QUESTION:
What shoudl be in my_custom_cck_type_load()?? How shall I grab all of the custom fields associated with the CCK type into the returned object? Or does drupal do this for me?
I'll use node_load as an example for this. Out of the box Drupal's menu system will load nodes at paths like.
node/1/view
node/2/view
node/3/view
(note: the "/view" part of the path is often hidden because it's the default tab)
The menu item setup to do this looks something like:
$items['node/%node/view'] = array(
'type' => MENU_DEFAULT_LOCAL_TASK,
'title' => 'blabla',
'page callback' => 'node_view',
'page arguments' => array(1)
/* more stuff */
);
In the real path the %node is replaced by a number, the NID of the node. This will cause menu to fire node_load using that number as the argument. We're also using %node as the page argument for the node_view callback. The argument that callback receives will be the fully loaded node object.

Drupal menu permissions question

I'm creating an admin module for my client that gives them access to some administration functionality concerning their content. I'm starting off by adding some permissions in my module by implementing hook_perm:
function mymodule_perm()
{
return array(
'manage projects',
);
}
I can then create my menu by adding to the admin section that already exists:
function mymodule_menu()
{
$items['admin/projects'] = array(
'title' => 'Projects',
'description' => 'Manage your projects.',
'page callback' => 'manage_projects_overview',
'access callback' => 'user_access',
'access arguments' => array('manage projects'),
'type' => MENU_NORMAL_ITEM,
'weight' => -100,
);
$items['admin/projects/add'] = array(
'title' => 'Add project',
'access arguments' => array('manage projects'),
'page callback' => 'mymodule_projects_add',
'type' => MENU_NORMAL_ITEM,
'weight' => 1,
);
return $items;
}
This will add a Projects section to the Administration area with an Add project sub section. All good.
The behavior I want is that my client can only see the Projects section when they log in. I've accomplished this by ticking the "manage projects" permission for authenticated users in the permissions section of my module. Now to give my client actual access to the Administration area I also need to tick "access administration pages" under the "system module" in the users permissions section. This works great, when I log in as my client I can only see the Projects section in the Administration area. There is one thing though, In my Navigation menu shown on the left column I can see the following items:
- Administer
- Projects
- Content management
- Site building
- Site configuration
- User management
I was expecting only the see Administer and Projects items, not the other ones. When I click e.g. Content Management I get a Content Management titled page with no sub-sections. Same for Site Building, Site Configuration and User Management. What's really odd is that Reports is not being shown which is also a top level Administration section.
Why are these other items, besides my Projects section, being shown and how can I make them stop from appearing if I'm not logged in as an administrator?
Your problem is that they are allowed to view those pages.
From the system module's hook_menu:
$items['admin/build'] = array(
'title' => 'Site building',
'description' => 'Control how your site looks and feels.',
'position' => 'right',
'weight' => -10,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
);
So when you gave them access administration pages you gave them access to the site building section, but not any item in it. A quick way to solve this is to:
Use hook_menu_alter to change the access settings for those menu items to something they don't have access to. Either make your own perm or use an existing one.
You could also use your theme to just hide the items.
I'm not sure exactly why the menu router displays those. But I may be able to help...
Why don't you change your path to something like:
projects/add
projects/%/edit
This is similar to the node module's menu hook. It may not be exactly what you're looking for but if you don't want these user's having access to admin stuff it could be the right way.

Resources