I wanted to write my own Mail class to display e-mails to the screen rather than actually send them out. I copied core's core/lib/Drupal/Core/Mail/VariableLog.php into my modules/custom/lib/Drupal/custom/Mail/DebugLog.php:

namespace Drupal\custom\Mail;

use Drupal\Core\Mail\PhpMail;

class DebugLog extends PhpMail implements MailInterface {

  /**
   * Overrides Drupal\Core\Mail\PhpMail::mail().
   *
   * Accepts an e-mail message and displays it on screen.
   */
  public function mail(array $message) {
    $string = check_plain(print_r($message, TRUE));
    $string = '<pre>' . $string . '</pre>';

    if (module_exists('devel')) {
      dpm($message, 'mail');
    }
    else {
      drupal_set_message('mail: ' . $string);
    }

    // Don't actually use debug to display the message since we want to be able
    // to categorize the watchdog type.
    watchdog('mail', $string, NULL, WATCHDOG_INFO);

    return TRUE;
  }
}

I went to admin/modules to enable my Custom module (which is an empty .module PHP file, info file and this class only), and I got the following:

Error message

Notice: Object of class Drupal\block\Plugin\Core\Entity\Block to string conversion in Drupal\Core\Database\Statement->execute() (line 58 of core/lib/Drupal/Core/Database/Statement.php).
Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined: SELECT revision.revision_id AS revision_id, revision.log AS log, revision.info AS info, base.id AS id, base.uuid AS uuid, base.type AS type, base.langcode AS langcode, base.revision_id = revision.revision_id AS isDefaultRevision FROM {custom_block} base INNER JOIN {custom_block_revision} revision ON revision.revision_id = base.revision_id WHERE (base.id IN (:db_condition_placeholder_0_bartik.content, :db_condition_placeholder_0_bartik.footer, :db_condition_placeholder_0_bartik.help, :db_condition_placeholder_0_bartik.login, :db_condition_placeholder_0_bartik.powered, :db_condition_placeholder_0_bartik.search, :db_condition_placeholder_0_bartik.tools, :db_condition_placeholder_0_seven.content, :db_condition_placeholder_0_seven.help, :db_condition_placeholder_0_seven.login)) ; Array ( [:db_condition_placeholder_0_bartik.content] => Drupal\block\Plugin\Core\Entity\Block Object ( [id] => bartik.content [label] => [uuid] => c41b5eb5-5313-465b-a703-3ccacd7cb2a3 [settings:protected] => Array ( [cache] => -1 ) [instance:protected] => [region:protected] => content [visibility:protected] => Array ( [path] => Array ( [visibility] => 0 [pages] => ) [role] => Array ( [roles] => Array ( ) ) [node_type] => Array ( [types] => Array ( [article] => 0 [page] => 0 ) ) [visibility__active_tab] => edit-visibility-path ) [weight:protected] => 0 [module:protected] => system [plugin:protected] => system_main_block [originalID:protected] => bartik.content [status] => 1 [langcode] => und [entityType:protected] => block [enforceIsNew:protected] => [newRevision:protected] => [isDefaultRevision:protected] => 1 [rdf_mapping] => Array ( ) ) [:db_condition_placeholder_0_bartik.footer] => Drupal\block\Plugin\Core\Entity\Block Object ( [id] => bartik.footer [label] => Footer menu [uuid] => b249731c-c12a-4985-aa00-0a99c4cfb06e [settings:protected] => Array ( [cache] => -1 ) [instance:protected] => [region:protected] => footer [visibility:protected] => Array ( [path] => Array ( [visibility] => 0 [pages] => ) [role] => Array ( [roles] => Array ( ) ) [node_type] => Array ( [types] => Array ( [article] => 0 [page] => 0 ) ) [visibility__active_tab] => edit-visibility-path ) [weight:protected] => 0 [module:protected] => system [plugin:protected] => system_menu_block:menu-footer [originalID:protected] => bartik.footer [status] => 1 [langcode] => und [entityType:protected] => block [enforceIsNew:protected] => [newRevision:protected] => [isDefaultRevision:protected] => 1 [rdf_mapping] => Array ( ) ) [:db_condition_placeholder_0_bartik.help] => Drupal\block\Plugin\Core\Entity\Block Object ( [id] => bartik.help [label] => [uuid] => 0bf722fc-815f-4770-a609-9c2ceceaf30b [settings:protected] => Array ( [cache] => -1 ) [instance:protected] => [region:protected] => help [visibility:protected] => Array ( [path] => Array ( [visibility] => 0 [pages] => ) [role] => Array ( [roles] => Array ( ) ) [node_type] => Array ( [types] => Array ( [article] => 0 [page] => 0 ) ) [visibility__active_tab] => edit-visibility-path ) [weight:protected] => 0 [module:protected] => system [plugin:protected] => system_help_block [originalID:protected] => bartik.help [status] => 1 [langcode] => und [entityType:protected] => block [enforceIsNew:protected] => [newRevision:protected] => [isDefaultRevision:protected] => 1 [rdf_mapping] => Array ( ) ) [:db_condition_placeholder_0_bartik.login] => Drupal\block\Plugin\Core\Entity\Block Object ( [id] => bartik.login [label] => User login [uuid] => 599fa142-924c-4872-a702-02fb822957c0 [settings:protected] => Array ( [cache] => -1 ) [instance:protected] => [region:protected] => sidebar_first [visibility:protected] => Array ( [path] => Array ( [visibility] => 0 [pages] => ) [role] => Array ( [roles] => Array ( ) ) [node_type] => Array ( [types] => Array ( [article] => 0 [page] => 0 ) ) [visibility__active_tab] => edit-visibility-path ) [weight:protected] => 0 [module:protected] => user [plugin:protected] => user_login_block [originalID:protected] => bartik.login [status] => 1 [langcode] => und [entityType:protected] => block [enforceIsNew:protected] => [newRevision:protected] => [isDefaultRevision:protected] => 1 [rdf_mapping] => Array ( ) ) [:db_condition_placeholder_0_bartik.powered] => Drupal\block\Plugin\Core\Entity\Block Object ( [id] => bartik.powered [label] => [uuid] => 04bc310e-760e-446e-b300-d5eca4bcf4f5 [settings:protected] => Array ( [cache] => -1 ) [instance:protected] => [region:protected] => footer [visibility:protected] => Array ( [path] => Array ( [visibility] => 0 [pages] => ) [role] => Array ( [roles] => Array ( ) ) [node_type] => Array ( [types] => Array ( [article] => 0 [page] => 0 ) ) [visibility__active_tab] => edit-visibility-path ) [weight:protected] => 10 [module:protected] => system [plugin:protected] => system_powered_by_block [originalID:protected] => bartik.powered [status] => 1 [langcode] => und [entityType:protected] => block [enforceIsNew:protected] => [newRevision:protected] => [isDefaultRevision:protected] => 1 [rdf_mapping] => Array ( ) ) [:db_condition_placeholder_0_bartik.search] => Drupal\block\Plugin\Core\Entity\Block Object ( [id] => bartik.search [label] => Search [uuid] => 11406905-7a7a-4aaa-8a00-fa1380a312ca [settings:protected] => Array ( [cache] => -1 ) [instance:protected] => [region:protected] => sidebar_first [visibility:protected] => Array ( [path] => Array ( [visibility] => 0 [pages] => ) [role] => Array ( [roles] => Array ( ) ) [node_type] => Array ( [types] => Array ( [article] => 0 [page] => 0 ) ) [visibility__active_tab] => edit-visibility-path ) [weight:protected] => -1 [module:protected] => search [plugin:protected] => search_form_block [originalID:protected] => bartik.search [status] => 1 [langcode] => und [entityType:protected] => block [enforceIsNew:protected] => [newRevision:protected] => [isDefaultRevision:protected] => 1 [rdf_mapping] => Array ( ) ) [:db_condition_placeholder_0_bartik.tools] => Drupal\block\Plugin\Core\Entity\Block Object ( [id] => bartik.tools [label] => Tools [uuid] => 3a585b73-db4c-4140-8a5b-91d7244257b9 [settings:protected] => Array ( [cache] => -1 ) [instance:protected] => [region:protected] => sidebar_first [visibility:protected] => Array ( [path] => Array ( [visibility] => 0 [pages] => ) [role] => Array ( [roles] => Array ( ) ) [node_type] => Array ( [types] => Array ( [article] => 0 [page] => 0 ) ) [visibility__active_tab] => edit-visibility-path ) [weight:protected] => 0 [module:protected] => system [plugin:protected] => system_menu_block:menu-tools [originalID:protected] => bartik.tools [status] => 1 [langcode] => und [entityType:protected] => block [enforceIsNew:protected] => [newRevision:protected] => [isDefaultRevision:protected] => 1 [rdf_mapping] => Array ( ) ) [:db_condition_placeholder_0_seven.content] => Drupal\block\Plugin\Core\Entity\Block Object ( [id] => seven.content [label] => [uuid] => 844a39df-4e2d-4664-ae05-5f287dfe083a [settings:protected] => Array ( [cache] => -1 ) [instance:protected] => [region:protected] => content [visibility:protected] => Array ( [path] => Array ( [visibility] => 0 [pages] => ) [role] => Array ( [roles] => Array ( ) ) [node_type] => Array ( [types] => Array ( [article] => 0 [page] => 0 ) ) [visibility__active_tab] => edit-visibility-path ) [weight:protected] => 0 [module:protected] => system [plugin:protected] => system_main_block [originalID:protected] => seven.content [status] => 1 [langcode] => und [entityType:protected] => block [enforceIsNew:protected] => [newRevision:protected] => [isDefaultRevision:protected] => 1 [rdf_mapping] => Array ( ) ) [:db_condition_placeholder_0_seven.help] => Drupal\block\Plugin\Core\Entity\Block Object ( [id] => seven.help [label] => [uuid] => a5ab9e31-4a9d-4fc3-9f00-ec8a82176e5b [settings:protected] => Array ( [cache] => -1 ) [instance:protected] => [region:protected] => help [visibility:protected] => Array ( [path] => Array ( [visibility] => 0 [pages] => ) [role] => Array ( [roles] => Array ( ) ) [node_type] => Array ( [types] => Array ( [article] => 0 [page] => 0 ) ) [visibility__active_tab] => edit-visibility-path ) [weight:protected] => 0 [module:protected] => system [plugin:protected] => system_help_block [originalID:protected] => seven.help [status] => 1 [langcode] => und [entityType:protected] => block [enforceIsNew:protected] => [newRevision:protected] => [isDefaultRevision:protected] => 1 [rdf_mapping] => Array ( ) ) [:db_condition_placeholder_0_seven.login] => Drupal\block\Plugin\Core\Entity\Block Object ( [id] => seven.login [label] => User login [uuid] => d1ce8654-7280-46c2-b600-fa22d55532b9 [settings:protected] => Array ( [cache] => -1 ) [instance:protected] => [region:protected] => content [visibility:protected] => Array ( [path] => Array ( [visibility] => 0 [pages] => ) [role] => Array ( [roles] => Array ( ) ) [node_type] => Array ( [types] => Array ( [article] => 0 [page] => 0 ) ) [visibility__active_tab] => edit-visibility-path ) [weight:protected] => 10 [module:protected] => user [plugin:protected] => user_login_block [originalID:protected] => seven.login [status] => 1 [langcode] => und [entityType:protected] => block [enforceIsNew:protected] => [newRevision:protected] => [isDefaultRevision:protected] => 1 [rdf_mapping] => Array ( ) ) ) in Drupal\Core\Entity\DatabaseStorageController->load() (line 198 of /home/dave/Dropbox/Projects/drupal8dev/core/lib/Drupal/Core/Entity/DatabaseStorageController.php).
The website has encountered an error. Please try again later.

How in the hell was I supposed to know my file was missing use Drupal\Core\Mail\MailInterface?

This was complicated by the fact that core's VariableLog doesn't add use statements because it's in the same namespace as PhpMail and MailInterface. I didn't know that you could do that and I thought you *always* had to list every class you refer to in your use statements, since that makes sense to me (all or nothing, not halfway between). For anyone new to namespaces, which is going to happen with Drupal 8, a lot, this is going to be an additional DX WTF.

Comments

Dave Reid’s picture

It appears that adding use Drupal\Core\Mail\MailInterface doesn't even resolve this. So I'm completely at a loss as to what the problem even is.

Crell’s picture

There's something else going on here besides a use statement. Not use'ing MailInterface should give you a fatal when you try to instantiate that class, but otherwise have no effect.

The error message looks like something is trying to pass a Block object as a database parameter, which is of course completely wrong. Try debugging from line 198 of DatabaseStorageController and see what the backtrace looks like.

Dave Reid’s picture

This is a duplicate of #1920862: Rename custom_block.module to block_content.module. I had custom module and core provides a custom_block module with an API function of custom_block_load, which when adding a custom module to your site becomes a hook implementation of hook_block_load()........

A death of a thousand suns should be administered to custom_block namespace.

Dave Reid’s picture

Status: Active » Closed (duplicate)