"Undefined property: stdClass" - drupal

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.

Related

"Syntax error or access violation" - in migration from Schema:: facade, but statement executes without error

I have a migration written that executes just fine on my test database - did dozens of tests runs without issue. I run it on a clone of my prod database and all of a sudden I'm having all sorts of problems. I'm beginning to think its a database config or permissions issue, but I'm logged into this clone as root, so I'm not even sure where to start looking...
If I copy the mysql statement from the error (...and fix the missing data) the statement executes without issue.
ALTER TABLE `retail_items-tmp_09-10-2020` CHANGE original_inventory initial_inventory INT DEFAULT NULL;
The offending line:
Schema::table('retail_items-tmp_09-10-2020', function($table) {
$table->renameColumn('original_inventory','initial_inventory');
});
The Error:
[PDOException (42000)]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-tmp_09-10-2020 CHANGE original_inventory initial_inventory INT DEFAULT NULL' at line 1
The migration:
public function up()
{
/* 1. Backup Existing Tables */
DB::statement('CREATE TABLE `retail_items-tmp_09-10-2020` LIKE `retail_items`; ');
DB::statement('CREATE TABLE `ARCH__retail_items_09-10-2020` LIKE `retail_items`; ');
DB::statement('INSERT INTO `retail_items-tmp_09-10-2020` SELECT * FROM `retail_items`; ');
DB::statement('INSERT INTO `ARCH__retail_items_09-10-2020` SELECT * FROM `retail_items`;');
/* 2. Update structure */
Schema::table('retail_items-tmp_09-10-2020', function($table) {
$table->renameColumn('original_inventory','initial_inventory');
});
Schema::table('retail_items-tmp_09-10-2020', function($table) {
$table->integer('event_ID')->length(11)->unsigned()->nullable();
$table->foreign('event_ID')->references('event_ID')->on('events');
});
/* 3. Update structure that would have been destructive prior to step 3 */
// When I had this piece of code included, it resulted in the same error "Syntax error or access violation..." this worked in testing, but throws errors on Prod, changed to DB:statement below with success.
// Schema::table('retail_items-tmp_09-10-2020', function($table) {
// $table->smallInteger('flag')->unsigned()->nullable(false)->default(0)->change();
// });
$query = "ALTER TABLE `retail_items-tmp_09-10-2020` CHANGE `flag` `flag` SMALLINT(11) DEFAULT 0 NOT NULL; ";
DB::statement($query);
}
I've been stuck on this for a bit, so any theories, places to look, etc would be appreciated.
(I have another migration that renames the temp table at the end of this. I have a few migrations and data operations that all together take ~ 10+ min to execute with the piece of code that I'm launching this with, so the temp tables are necessary to prevent downtime when launching in production)
I still ended up having more troubleshooting/ other roadblocks on this issue, so this only removed one...
Even though they were properly quoted, I read a number of S.O. posts on the subject and as Webtect suggests I did end up simply removing all the dashes and things worked much more consistently and reliably. I'm still feel like it's a mystery as to why this happened because I've used hyphens in dates like this before - properly quoted - without issue. But this one sure threw me for a loop. I think I will entirely stop the practice of using hyphens in dated-table-names going forward. They seem to cause more headaches than the improved readability warrants.

When trying to preview a theme - The site is experiencing technical difficulties. Please check your site admin email inbox for instructions

When trying to preview a new theme for my blog, this appears...
The site is experiencing technical difficulties. Please check your site admin email inbox for instructions.
I didn't receive an email though. I've tried deactivating all of my plugins and this didn't work. Any ideas? I'm new to this.
You should detect the error before do anything.
Turn on WP debug mode and you will see what error do you have:
In wp-config.php:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );
More information about WP debugging: https://wordpress.org/support/article/debugging-in-wordpress/
Website/Admin is displaying "The site is experiencing technical difficulties. Please check your site admin email inbox for instructions."
This was my solution for our website. I only fixed enough issues for the admin page to start working again.
This was all done after I fixed my initial site issues that crashed the entire site, addressed in this question: https://stackoverflow.com/a/65357857/14850513
After I made those fixes, the admin page was still giving the previously stated error message that you described, so here is how I fixed it. The following bullets are all of the error messages I faced and how I fixed them. This may not solve everyone's issues but these were the specific ones I faced.
Go to the public.html/wp-config.php
go to the following line and change debugging to true
define('WP_DEBUG', false); #Change to true
This will show all of the errors that are causing the site to have issues. You must address them until it begins to function again. You will know because it will start working the next time you refresh.
ERROR 1: Array and string offset access syntax with curly braces is deprecated
Follow to each line listed and change '{}' to '[]'
ERROR 2: Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home1/seth/public_html/wp-content/plugins/revslider/includes/operations.class.php on line 2364
I open the php file at the problem line, I changed the term [continue;] to [break;]
ERROR 3: "Deprecated: Function create_function() is deprecated in xxx.php" on 258 line
Changed
public static function registerWidget($widgetName){
add_action('widgets_init', create_function('', 'return register_widget("'.$widgetName.'");'));
}
to
public static function registerWidget($widgetName){
add_action('widgets_init', function () use ($widgetName) {
return register_widget($widgetName);
});
}
ERROR 4: Fatal error: Uncaught Error: [] operator not supported for strings
the failing line was:
self::$arrMetaBoxes[] = $box;
The fix is to add the following line right before it
self::$arrMetaBoxes = [];
My admin page started working at this point, but you may need to continue fixing errors until it begins to function properly again.

Drupal 8: Mismatched entity and/or field definitions

While trying to understand why my view is not displaying, I noticed the following error in the log:
I do not think it is possible to delete the URL alias from Taxonomy terms. At least I cannot find how to do this.
I have, however gone through ALL of my taxonomy terms and removed the value for this field.
I have also done the following with Pathauto:
Also, I have checked the report located at admin/reports/fields and can confirm that there are no entities that use a field called URL alias.
I have gone through each content item and ensured that they have the following setting (anyone know how to do this in bulk?). But still the error remains.
Anyone know then how I can fix this strange error?
Im not entirely sure what this command does, but it fixed the error:
drush updb --entity-updates
Since https://www.drupal.org/node/2554097, the magic in Drupal core that took care of updating entity definitions is gone. drush updb --entiy-updates is an alternative to this but it is not a silver bullet. Instead, it is safer to write database updates.
Taking the screenshot at the top as an example, here is a database update that would delete those two field definitions:
/**
* Fix taxonomy and node field definitions.
*
*/
function mymodule_update_8101() {
$manager = \Drupal::entityDefinitionUpdateManager();
if ($field = $manager->getFieldStorageDefinition('alias', 'node')) {
$manager->uninstallFieldStorageDefinition($field);
}
if ($field = $manager->getFieldStorageDefinition('alias', 'term')) {
$manager->uninstallFieldStorageDefinition($field);
}
}
Have a look at the rest of the available methods at https://www.drupal.org/node/2554097 in order to write database updates for each scenario.
use the entity_update module or the devel_entity_updates module

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.

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