How to throw 404 error in Drupal module? - drupal

I need to throw 404 error in module. Or may be there are any possibility to set required option for menu hook?

It is easy. These should take care of watchdog, HTTP 404 response code and other related things.
For Drupal 6 & 7
In your module's page callback, do: return drupal_not_found();
For Drupal 8
In the class::method() referred to in the _controller definition (i.e. the page callback or the method responsible for generating output for the request), do:
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
References
drupal_not_found() documentation for Drupal 6.
drupal_not_found() documentation for Drupal 7.
NotFoundHttpException documentation for Drupal 8.

For Drupal 8+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
// then
throw new NotFoundHttpException();

MENU_NOT_FOUND should be returned in page callback functions.
Page callback functions wanting to report a "page not found" message should return MENU_NOT_FOUND instead of calling drupal_not_found().
— http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_not_found/7

Look into the drupal_add_http_header() function to play with the HTTP header attributes. Also make sure you stick this at the top of your module's code to make sure it executes first. Also, you might find this helpful. https://www.drupal.org/project/generate_errors

Related

Play Framework - how do I redirect to a page generated by a plugin controller? [SecureSocial]

I have this in my routes.conf file:
# SecureSocial routes
# Login page
GET /login securesocial.controllers.LoginPage.login
And then I have this in a Scala Controller file
val index = SecuredAction { implicit request =>
Redirect(routes.UserOps.watchlist)
// how do I go straight to /login? >|
}
Doing this takes me to a login page that has a red error bar saying "you must be signed in to view this page". If I access `localhost:9000/login' I get the login page without the error bar.
Normally I do a Redirect(routes.SomeControllerObject.ActionMethodName) but in this case the controller I need to access is in a plugin...
I feel like I'm missing something rather large here...
Update:
To reverse-route to an action in a plugin controller you need to provide the correct, full path to the plugin's routes class.
Redirect(securesocial.controllers.routes.LoginPage.login)
Original Answer
For reverse-routing I don't think it matters where the Controller is since Play is building that when the project compiles. From the documentation:
For each controller used in the routes file, the router will generate a ‘reverse controller’ in the routes package, having the same action methods, with the same signature, but returning a play.api.mvc.Call instead of a play.api.mvc.Action.
So this should work just as if LoginPage was a controller directly in your app.
Redirect(routes.LoginPage.login);
In order to show the message, you must have an item in the Flash. When you hit /login directly, there is no flash, so you won't see that message.
If you want to see that message:
Redirect(securesocial.controllers.LoginPage.login).flashing( ... )

drupal module redirect destination issue

I am working on yet another module idea. This module is simply supposed to redirect a user automatically to a specified node like in the following example code:
function test_module_init(){
drupal_goto('node/100');
}
The code is a mere example. But, the very really problem is the code keeps executing until the browser returns a "too many redirects" error message.
I understand why this is happening. What I need help with is the best hook to place my drupal_goto code so it executes once instead of the multiple times it currently does in hook_init.
Can't you just do something like this:
function test_module_init() {
if(isset($_GET['q') && $_GET['q'] == 'node/100') {
// skip goto statement
return;
}
drupal_goto('node/100');
}
The init hook you wrote fires on every page load including when you are on node/100. That's why you're getting a redirect. So the page is redirecting to itself. You really only need to redirect if you're not already on node/100. You can find this info in $_GET['q'] if you need to.
You don't need a different hook you just need to make sure that you don't call the drupal_goto if you're already on the destination page.

Drupal error page

I would like to create a custom error page in Drupal 7. There are things like set_message, but they don't log the errors. So is there any hook or something similar to catch the error, log it and display a human error to my users?
You stated above that your goal is "catch the error, log it and display a human error to my users".
In that case you're probably looking for the functionality Try/Catch which allows you to try to run a block of code and if something goes wrong it will display a message.
In your particular case you can log the error to Drupal's database logging system with the watchdog function http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/watchdog/7
Optionally you could also log this to the PHP error_log as well see http://php.net/manual/en/function.error-log.php
Then you could continue on displaying the message to the user using the drupal_set_message function that you already figured out.
The final code for what you're trying to accomplish would look something like this:
try {
// RUN YOUR CUSTOM CODE HERE
} catch (Exception $e) {
// Record the error Drupal's database log
watchdog('error_page', $e->getMessage());
// Record the error to PHP's error_log
error_log($e->getMessage());
// Display a message to the user
drupal_set_message("We're sorry, but we couldn't find the page you were looking for.", 'error');
}
drupal_get_messages() could be used to fetch an array to iterate through for the the error types of messages.
I'm not sure I made 100% sense of your question, but since you referenced drupal_set_message() i thought this might be what you were looking for.
You could handle it in hook_init(), check for messages there, if you find any, do something with it.
Redirecting on an error though could potentially break default drupal functionality like forms.

"Undefined property: stdClass"

This error suddenly occurred when Pressflow was added to our Drupal installation. It is coming from a custom module that, prior to Pressflow seemed to work fine. After the addition of Pressflow, running Drush causes this error to display on screen.
The function that is identified as the source of the error looks like this:
function user_search_location_get(&$user) {
if (count($user->user_location_pref)) { // This line is causing the error.
return $user->user_location_pref;
}
// …
}
The error message is the following:
WD php: Notice: Undefined property: stdClass::$user_location_pref in user_search_location_get()
Short answer, in your custom module, you should check if that property exists before you count it. That, or make sure the $user object has that property before you use it.
if (isset($user->user_location_pref) && count($user->user_locaion_pref) > 0) {
return $user->user_locaion_pref;
}
While it is a little more work, when you start developing with notices turned on, you will find errors in your code that otherwise would have not appeared till later, and would have been more difficult to track down.
In your previous environment or install, the PHP error reporting was probably set to not show notices. While I recommend keeping notices on and making your code work with them, you can turn them off through the Drupal 7 UI. Configuration -> Development -> Logging and Errors. Then set the value to 'Errors and Warnings' ... Otherwise, you can set your error reporting level in your php.ini to report all except notices.
Note that Drupal 6 did not force notice reporting on, while Drupal 7 does. That prompts this type of question a lot.
If this is your only notice issue though, it makes more sense to just correct your custom module.

Correct way to force an invoice e-mail to be sent to a user in UberCart?

What is the correct way to force the system to send an invoice to a client. I'm trying to use:
uc_order_action_email($order, $settings);
But I keep getting:
Fatal error: Call to undefined function uc_price() in C:\xampp\htdocs\YourEstablishment\src\sites\all\modules\ubercart\payment\uc_payment\uc_payment.module on line 149
It might be a flaw in the module. The function that it's complaining about, uc_price, is defined in
ubercart/uc_store/includes/uc_price.ini
Since it's located in a ini file, that means that drupal wont include it by it self. I'm not familiar with ubercart, since I've never used it, but it seems like this could be a bug in the module. If no one here can come up with an explanation, you should go to the issue tracker.
A quick fix to your problem would be to add this before you call the function
require_once(drupal_get_path('module', 'uc_store') . '/includes/uc_price.inc');
it will include the needed file.

Resources