我正在使用SHS模块。我想要做的是,覆盖SHS小部件,以便我可以改变用于获取小部件中的数据的查询。直到现在,我已经覆盖了路由控制器,并且发现基础查询是从loadTree()
文件的core/modules/taxonomy/src/TermStorage.php
函数生成的。
现在基本上我想要的是,覆盖这个函数,这样我就可以根据我的要求改变查询。我不确定在OOPS中这样做的正确方法是什么。
请帮忙。
答案 0 :(得分:0)
您指的是this line吗?如果是这样和你已经通过创建自己的控制器副本来覆盖路由,那么你应该能够在这里添加自己的查询,该查询返回\Drupal\taxonomy\TermInterface
个对象的数组{ {3}}
要回答你是否可以覆盖\Drupal\taxonomy\TermStorage::loadTree()
的问题,我不知道如何进行简单的交换,即使有,也可以覆盖这样的核心功能可能会导致系统中其他地方的不稳定性。通过覆盖路线控制器听起来像是在正确的轨道上。需要明确的是,完全覆盖该控制器以便SHS(或任何)模块可以使用的步骤是:
此时,您可以修改自定义路由控制器(包括查询)以返回您想要的任何内容。
编辑:
以下是使用名为my_module
的自定义模块如何执行此操作的代码示例。在这个例子中,我只是修改查询以返回名称为my_vocabulary
的vid Term Name
的条款,但这里的要点是您可以将其修改为您想要的任何查询。就像免责声明一样,我没有测试这段代码,但我在自己的网站上运行了类似的结构化代码。
my_module.routing.yml
my_module.term_data:
path: '/my-module-term-data/{identifier}/{bundle}/{entity_id}'
defaults:
_controller: '\Drupal\my_module\Controller\MyModuleShsController::getTermData'
_title: 'Get term data'
entity_id: null
requirements:
_permission: 'access content'
my_module.module
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function my_module_field_widget_options_shs_form_alter(&$element, FormStateInterface $form_state, $context) {
$element['#shs']['baseUrl'] = 'my-module-term-data';
}
SRC /控制器/ myModuleShsController.php
<?php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\shs\Cache\ShsCacheableJsonResponse;
use Drupal\shs\Cache\ShsTermCacheDependency;
/**
* Controller for getting taxonomy terms.
*/
class MyModuleShsController extends ControllerBase {
/**
* Load term data.
*
* @param string $identifier
* Name of field to load the data for.
* @param string $bundle
* Bundle (vocabulary) identifier to limit the return list to a specific
* bundle.
* @param integer $entity_id
* Id of parent term to load all children for. Defaults to 0.
*
* @return CacheableJsonResponse
* Cacheable Json response.
*/
public function getTermData($identifier, $bundle, $entity_id = 0) {
$context = [
'identifier' => $identifier,
'bundle' => $bundle,
'parent' => $entity_id,
];
$response = new ShsCacheableJsonResponse($context);
$cache_tags = [];
$result = [];
$langcode_current = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
$entity_manager = \Drupal::getContainer()->get('entity.manager');
$storage = $entity_manager->getStorage('taxonomy_term');
$translation_enabled = FALSE;
if (\Drupal::moduleHandler()->moduleExists('content_translation')) {
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $translation_manager */
$translation_manager = \Drupal::service('content_translation.manager');
// If translation is enabled for the vocabulary, we need to load the full
// term objects to get the translation for the current language.
$translation_enabled = $translation_manager->isEnabled('taxonomy_term', $bundle);
}
// CUSTOM CODE STARTS HERE.
$terms = $storage->loadByProperties(['vid' => 'my_vocabulary', 'name' => 'Term Name']);
// CUSTOM CODE ENDS HERE.
foreach ($terms as $term) {
$langcode = $langcode_current;
if ($translation_enabled && $term->hasTranslation($langcode)) {
$term = $term->getTranslation($langcode);
}
else {
$langcode = $term->default_langcode;
}
$tid = $translation_enabled ? $term->id() : $term->tid;
$result[] = (object) [
'tid' => $tid,
'name' => $translation_enabled ? $term->getName() : $term->name,
'description__value' => $translation_enabled ? $term->getDescription() : $term->description__value,
'langcode' => $langcode,
'hasChildren' => shs_term_has_children($tid),
];
$cache_tags[] = sprintf('taxonomy_term:%d', $tid);
}
$response->addCacheableDependency(new ShsTermCacheDependency($cache_tags));
$response->setData($result, TRUE);
return $response;
}
}