I'm working on a site where there will be several admin pages that have intense logic in them. Let's take an example. I want a page that lists certain nodes and allows you to add or remove them, or do other actions on them.
Now, I can create a normal page in Drupal and select the input filter as "php code" and then just add the following line:
<?php executeTheCodeThatDoesEverythingForThisPage(); ?>
And I can put this method in a module.
However, this is not ideal, as a DB call is still made to find out what method must be called. I want to:
Entirely skip the DB call
Have a folder, e.g. pages, that stores all my custom pages, e.g. aboutus.php, mystuff.php
Still have access to all the drupal functions in this file
Anybody know how I can do this?
<<-- EDITED -->>
If I can't skip the DB query, can someone tell me the best way to include data from a static php file? Is there a module for this, or should I just add the following code:
include_once "myFile.php";
To include files, you need to look at the menu system, in particular page callbacks:
http://drupal.org/node/146172
Supposing that the myFile.php file is copied in the directory containing the module itself, you could use code similar to the following one.
function mymodule_menu() {
$items['mymodule_path'] = array(
'page callback' => 'mymodule_path_output',
'access arguments' => array('access content'),
);
return $items;
}
function mymodule_path_output() {
module_load_include('php', 'mymodule', 'myFile');
// …
}
The implementation of hook_menu() I used is minimal; see the documentation for more information about the hook.
Related
I've recently upgraded a site to Drupal 7.59 with install profile:
Commerce Kickstart (commerce_kickstart-7.x-2.54)
Previously there was a function that had been added to the core which has now been removed because of the upgrade. This shouldn't have been added to the core and I'm not sure why it was. I've added this function back in and its not doing what it did previously so I'm not sure what other changes I would need to make to get it to work.
Here's the function which is found in /profiles/commerce_kickstart/themes/commerce_kickstart_admin/template.php -
function commerce_kickstart_admin_commerce_price_formatted_components($variables) {
// Add the CSS styling to the table.
drupal_add_css(drupal_get_path('module', 'commerce_price') . '/theme/commerce_price.theme.css');
// Build table rows out of the components.
$rows = array();
foreach ($variables['components'] as $name => $component) {
$rows[] = array(
'data' => array(
array(
'data' => $component['title'],
'class' => array('component-title'),
),
array(
'data' => $component['formatted_price'],
'class' => array('component-total'),
),
),
'class' => array(drupal_html_class('component-type-' . $name)),
);
}
if($variables['components']['discount']['price']['amount']){
unset($rows[0]);
unset($rows[2]);
}else{
$rows = array_splice($rows, 2);
}
return theme('table', array('rows' => $rows, 'attributes' => array('class' => array('commerce-price-formatted-components'))));
}
Can anyone give any pointers as to how to get this working? It doesn't appear to even be getting invoked.
Additional info from the comments:
it's a function in the profile?
yes
Was the function added afterwards (as in "Never hack core")?
Yes, looks like it.
Or was it removed by the maintainers?
Doesn't look like this was ever part of any official release
Do you use some version control system like Git?
Yes. This function was added on 14/05/2015 12:18 according to the repo.
Have you checked the profile's release notes and issue queue?
Had a look but don't see anything.
Thanks for adding the extra info!
Well, if this really was custom code than it should never have been added to the profile in the first place. Never ever add custom code to any core or contrib file. As it's going to be deleted as soon as you update. Like it has happened to you.
I guess the most important part of this custom function was drupal_add_css(drupal_get_path('module', 'commerce_price') . '/theme/commerce_price.theme.css'); and that this commerce_price.theme.css maybe also got deleted.
Apart from that it's hard to tell from far and I'm not an expert in the commerce module. So, what I would do now is to narrow down the issue systematically.
Restore your repo to a time in history before this module got updated and get the site running.
Find out what this code is doing exactly, what other functions or flows are involved. Maybe with the help of the Devel module and the mighty dpm() function.
Try to rebuild the custom code from the profile in a custom module.
Then reset the repo to the current state and see if your custom module's code is still firing. If not, debug it to match the updated profile's code.
Apart from that, find the person who added the code and ask them why and what this code is doing. And tell them to never ever again hack core or contrib code :)
Good luck!
I am currently working on a site which is on smarty based.The name of the site is http://example.com
I built a new folder in the root path and installed droupon (which is a component of drupal for buying or creating any deal) on the folder.The site url is http://example.com/coupon
Now I want to integrate or merge this two sites.So that when a registered user access example.com then he can access the example.com/coupon with his session user id.
But this is the problem.
Is this really possible to pass data from smarty based site (example.com) to drupal site example.com/coupon ?
Please help me.
I would write at module in Drupal that looks at $_SESSION and creates and/or login the user at the Drupal-site. Perhaps the rules module can do that work, but you will probably need to implement a rules-hook to grab the relevant session data as input to the rules component.
Here are a few lines of code that do some of the work but you need to implement hook_menu aswell to register an entrypoint for the integration.
//register user
$passwd = user_password();
$edit = array(
'name' => $_SESSION['username'],
'pass' => $passwd,
'mail' => $_SESSION['email'],
'init' => $_SESSION['email'],
'status' => 1,
'access' => REQUEST_TIME,
);
$uu = drupal_anonymous_user();
$u = user_save($uu, $edit);
//Login
global $user;
$user = user_load($u->uid);
$login_array = array ('name' => $username);
user_login_finalize($login_array);
However, Im not sure this is the best way to go about it. Sharing data in the same session-namespace between 2 different applications will probably lead to errors on both sides. Is it not better to implement the whole site in Drupal from the beginning?
I have a list of latest projects displaying on the home page of my wordpress site. I can see the section that calls for the projects, but not sure where I can reverse the order.
I know this works for posts.
<?php query_posts($query_string . "&order=ASC"); ?>
but don't know where to add it. This is the code that calls the projects:
$wpGrade_Options->get('homepage_portfolio_limit') ? $projects_nr = $wpGrade_Options->get('homepage_portfolio_limit') : $projects_nr = 3;
wpgrade_display_portfolio( $projects_nr, true, true); ?>
You are probably using plugin or a theme.
You need to find this function in the plugin/theme files: wpgrade_display_portfolio
From command line you would simply do:
grep -ir "wpgrade_display_portfolio" wordpress/dir
Inside that function there is either a direct call to DB, or if writer of that script was wise built in wp get_posts, WP_POSTS, or query_posts functions.
IF you find any of these wordpress native functions than you can easily reverse order by adding:
'order' => 'DESC',
If there is a mysql query, then I will have to see it first to give you meaningful answer. I also need to see DB and how you actually would like to order things.
I'm trying to import nodes from my forum to drupal 7. Not in bulk, but one by one so that news posts can be created and referenced back to the forum. The kicker is that I'm wanting to bring image attachments across as well...
So far, using the code example here http://drupal.org/node/889058#comment-3709802 things mostly work: Nodes are created, but the images don't go through any validation or processing.
I'd like the attached images to be validated against the rules defined in the content type. in particular the style associated with my image field which resizes them to 600x600.
So, instead of simply creating the nodes programatically with my own form, i decided to modify a "new" node using hook_node_prepare and using the existing form to create new content (based on passed in url args). This works really well and a create form is presented pre-filled with all my data. including the image! very cute.
I expected that i could then hit preview or save and all the validation and resizing would happen to my image, but instead i get the error:
"The file used in the Image field may not be referenced."
The reason for this is that my file doesn't have an entry in the file_usage table.. *le sigh*
so, how do i get to all the nice validation and processing which happens when i manually choose a file to upload? like resizing, an entry in the file_usage table.
The ajax upload function does it, but i can't find the code which is called to do this anywhere in the api.
What file upload / validation functions does Drupal call which i'm not doing?
Anybody have any experience with the file/image api for Drupal 7 who can help me out?
For getting the usage entry (in essence, checking out a file to a specific module so that it doesn't get deleted while its in use) look up the Drupal function 'file_usage_add()'
For validating incoming images, I got this example from user.module (if you're comfortable with PHP, you can always look at the core to see how something is done the 'Drupal way'):
function user_validate_picture(&$form, &$form_state) {
// If required, validate the uploaded picture.
$validators = array(
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);
// Save the file as a temporary file.
$file = file_save_upload('picture_upload', $validators);
if ($file === FALSE) {
form_set_error('picture_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('user_picture_path', 'pictures'))));
}
elseif ($file !== NULL) {
$form_state['values']['picture_upload'] = $file;
}
}
That function is added to the $form['#validate'] array like so:
$form['#validate'][] = 'user_validate_picture'
I have the following in my template.php file:
function theme098_theme() {
return array(
'email_node_form' => array(
'arguments' => array('form' => NULL),
)
);
}
and...
function theme098_email_node_form($form) {
return drupal_render($form);
}
I've excluded the code where i actually modify the form and cut it down so that no modifications happen. Two problems occur:
The order of items (i.e. their weights) is messed up. The save button is at the top etc. Even if I try to edit the form by setting the weight, the save button STILL appears at the top.
The real problem: Conditional fields doesn't work. For some reason, I think this overwrites what other modules are supposed to do? I'm not sure
Can anyone shed some light?
3 things.
In this case you should probably use hook_form_alter(), if you want to change the order or change the form, instead of using a theme function to alter it. Keep presentation and logic separated.
When you define theme functions with hook theme, you should call them theme_xxx instead of themename_/ modulename_.
Modules (and possible themes) have a weight weight that determines the order they are run with functions like hook_*_alter. Look at the install file for the devel module to see how this is done.
One or more of these things should help you out.