我有一个包含Drupal 8,Drupal Commerce 2.11和消息模块的站点。
我创建了一个消息模板,以在产品发布时创建消息:
<?php
/**
* @file
* Holds hook implementation for the Message Activity Stream module.
*/
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\group\Entity\Group;
use Drupal\node\Entity\Node;
use Drupal\comment\Entity\Comment;
use Drupal\commerce_store\Entity\Store;
use Drupal\commerce_product\Entity\Product;
use Drupal\user\Entity\User;
use Drupal\profile\Entity\Profile;
use Drupal\message\Entity\Message;
/**
* Implements hook_commerce_product_insert().
*/
function message_activity_stream_commerce_product_insert(Product $commerce_product) {
$message = Message::create(['template' => 'mas_create_product', 'uid' => $commerce_product->get('uid')]);
$message->set('field_product_reference', $commerce_product);
$message->set('field_published', $commerce_product->isPublished());
$message->save();
}
/**
* Implements hook_node_insert().
*/
function message_activity_stream_node_insert(Node $node) {
$message = Message::create(['template' => 'mas_create_node', 'uid' => $node->get('uid')]);
$message->set('field_node_reference', $node);
$message->set('field_published', $node->isPublished());
$message->save();
}
/**
* Implements hook_commerce_product_update().
*/
function message_activity_stream_commerce_product_update(Product $commerce_product) {
if (empty($commerce_product->original)) {
return;
}
message_activity_stream_update_message_status($commerce_product);
}
/**
* Implements hook_node_update().
*/
function message_activity_stream_node_update(Node $node) {
if (empty($node->original)) {
return;
}
message_activity_stream_update_message_status($node);
}
/**
* Set message entity published field when it changes in the related entity.
*
* @param \Drupal\Core\Entity\ContentEntityBase $entity
* The entity object.
*/
function message_activity_stream_update_message_status(ContentEntityBase $entity) {
if ($entity->isPublished() == $entity->original->isPublished()) {
return;
}
$query = \Drupal::entityQuery('message');
$field = 'field_' . $entity->getEntityType()->id() . '_reference';
$query->condition($field . '.target_id', $entity->id());
$results = $query->execute();
if (empty($results)) {
return;
}
$messages = Message::loadMultiple($results);
foreach ($messages as $message) {
$message->set('field_published', $entity->isPublished());
$message->save();
}
}
如果我通过发布注册产品并通过取消发布对其进行修改,则会显示错误:
网站遇到意外错误。请稍后再试。
Error: Call to a member function getColumns() on boolean in Drupal\Core\Entity\Query\Sql\Tables->addField() (line 236 of /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php) #0 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/Query/Sql/Condition.php(52): Drupal\Core\Entity\Query\Sql\Tables->addField('field_commerce_...', 'INNER', NULL) #1 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/Query/Sql/Query.php(162): Drupal\Core\Entity\Query\Sql\Condition->compile(Object(Drupal\Core\Database\Driver\mysql\Select)) #2 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/Query/Sql/Query.php(73): Drupal\Core\Entity\Query\Sql\Query->compile() #3 /var/www/www-domaine-com/web/modules/custom/message_activity_stream/message_activity_stream.module(122): Drupal\Core\Entity\Query\Sql\Query->execute() #4 /var/www/www-domaine-com/web/modules/custom/message_activity_stream/message_activity_stream.module(81): message_activity_stream_update_message_status(Object(Drupal\commerce_product\Entity\Product)) #5 [internal function]: message_activity_stream_commerce_product_update(Object(Drupal\commerce_product\Entity\Product)) #6 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(403): call_user_func_array('message_activit...', Array) #7 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(204): Drupal\Core\Extension\ModuleHandler->invokeAll('commerce_produc...', Array) #8 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(756): Drupal\Core\Entity\EntityStorageBase->invokeHook('update', Object(Drupal\commerce_product\Entity\Product)) #9 /var/www/www-domaine-com/web/modules/contrib/commerce/src/CommerceContentEntityStorage.php(92): Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('update', Object(Drupal\commerce_product\Entity\Product)) #10 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(507): Drupal\commerce\CommerceContentEntityStorage->invokeHook('update', Object(Drupal\commerce_product\Entity\Product)) #11 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(641): Drupal\Core\Entity\EntityStorageBase->doPostSave(Object(Drupal\commerce_product\Entity\Product), true) #12 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(432): Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object(Drupal\commerce_product\Entity\Product), true) #13 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(774): Drupal\Core\Entity\EntityStorageBase->save(Object(Drupal\commerce_product\Entity\Product)) #14 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Entity/Entity.php(390): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object(Drupal\commerce_product\Entity\Product)) #15 /var/www/www-domaine-com/web/modules/contrib/commerce/modules/product/src/Form/ProductForm.php(233): Drupal\Core\Entity\Entity->save() #16 [internal function]: Drupal\commerce_product\Form\ProductForm->save(Array, Object(Drupal\Core\Form\FormState)) #17 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Form/FormSubmitter.php(111): call_user_func_array(Array, Array) #18 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Form/FormSubmitter.php(51): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState)) #19 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Form/FormBuilder.php(589): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState)) #20 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Form/FormBuilder.php(318): Drupal\Core\Form\FormBuilder->processForm('commerce_produc...', Array, Object(Drupal\Core\Form\FormState)) #21 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Controller/FormController.php(93): Drupal\Core\Form\FormBuilder->buildForm('commerce_produc...', Object(Drupal\Core\Form\FormState)) #22 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch)) #23 /var/www/www-domaine-com/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #24 /var/www/www-domaine-com/web/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #25 /var/www/www-domaine-com/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #26 /var/www/www-domaine-com/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #27 /var/www/www-domaine-com/vendor/symfony/http-kernel/HttpKernel.php(151): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #28 /var/www/www-domaine-com/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #29 /var/www/www-domaine-com/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #30 /var/www/www-domaine-com/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #31 /var/www/www-domaine-com/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #32 /var/www/www-domaine-com/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) #33 /var/www/www-domaine-com/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #34 /var/www/www-domaine-com/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\ban\BanMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #35 /var/www/www-domaine-com/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #36 /var/www/www-domaine-com/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #37 /var/www/www-domaine-com/web/core/lib/Drupal/Core/DrupalKernel.php(669): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #38 /var/www/www-domaine-com/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #39 {main}.