我正在使用SHS模块。我想要做的是覆盖SHS小部件,这样我就可以修改用于获取小部件中数据的查询。到目前为止,我已经重写了路由控制器,并发现底层查询是从loadTree()文件的core/modules/taxonomy/src/TermStorage.php函数生成的。
现在,基本上我想要的是,覆盖这个函数,这样我就可以将查询更改为我的需求。我不知道怎么做才是正确的方式在OOPS。
请帮帮忙。
发布于 2018-06-13 14:48:00
你是指这条线吗?如果是这样,并且您已经通过创建这个控制器的自己的副本来覆盖该路由,那么您应该能够在这里添加您自己的查询,该查询将返回每个\Drupal\taxonomy\TermInterface对象的数组。
要回答您是否可以重写\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/Controller/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;
}
}https://stackoverflow.com/questions/50838108
复制相似问题