numero2/contao-tags 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

numero2/contao-tags

Composer 安装命令:

composer require numero2/contao-tags

包简介

Adds the possibility to assign tags to individual elements.

README 文档

README

About

Adds the possibility to assign tags to individual elements.

System requirements

Installation & Configuration

  • Install via Contao Manager or Composer (composer require numero2/contao-tags)

News

  • Add the following code snippet into your news_(full|latest|short|simple).html5 template

    <?php if( $this->tags ): ?>
      <div class="tags">
        <?php foreach( $this->tags as $tag ): ?>
          <?= $tag; ?>
        <?php endforeach; ?>
     </div>
    <?php endif; ?>
  • Configure your Newslist or Newsreader front end modules and set a Redirect page for tags (optional)

  • Additionally add the News Tag Cloud module anywhere on your page

Events

  • Add the following code snippet into your event_(full|list|teaser|upcoming).html5 template

    <?php if( $this->tags ): ?>
      <div class="tags">
        <?php foreach( $this->tags as $tag ): ?>
          <?= $tag; ?>
        <?php endforeach; ?>
     </div>
    <?php endif; ?>

    ⚠️ Note: Contao does not provide any Hooks for the Eventreader module, therefore we need to add some logic at the beginning of our template to parse the Tags correctly.

    <?php
    
    use Contao\ModuleEventReader;
    use Contao\ModuleModel;
    use Contao\System;
    
    $moduleModel = ModuleModel::findOneById(69); // replace with the ID of your actual EventReader module
    $module = new ModuleEventReader($moduleModel);
    
    $tagListener = System::getContainer()->get('numero2_tags.listener.events');
    $event = $tagListener->parseEvent($this->arrData, $module);
    
    $this->tags = $event['tags'];
    $this->tagsRaw = $event['tagsRaw'];
    
    ?>
  • Configure your Eventlist or Eventreader front end modules and set a Redirect page for tags (optional)

  • Additionally add the Events Tag Cloud module anywhere on your page

Insert-Tags

This extensions comes with a couple of Insert-Tags that can be used to link to a page which will only show entries with matching tags.

Insert-Tag Description
{{tag_link::1::foo}} Creates a link URL to the page with ID 1 and the tag foo
{{tag_link::1::foo::bar}} Creates a link URL to the page with ID 1 and the tags foo and bar
{{tag_link::1::foo|absolute}} Creates an absolute link URL to the page with ID 1 and the tag foo
{{tag_link::1::foo|get}} Creates a link URL to the page with ID 1 and the tag foo using GET parameters
{{tag_link::1::foo|absolute|get}} Creates an absolute link URL to the page with ID 1 and the tag foo using GET parameters
{{tags_active}} Creates a list of all active tags seperated by , , e.g. tag1, tag2, tag3
{{tags_active:: + }} Creates a list of all active tags seperated by the given string +, e.g. tag1 + tag2 + tag3
{{tags_active::, :: and }} Creates a list of all tags seperated by , but the last tag is added with and, e.g. tag1, tag2, tag3 and tag4

A more robust way for the links would be to use the tag's ID instead of its name (which can be changed under System › Tags). So instead of {{tag_link::1::foo}} you could also write {{tag_link::1::69}} (assuming the ID of foo is 69).

For Developers

Integrating a tags field in your own extension

If you want to use the tags for fields in your own Data Containers you can do so by defining the field as follows:

$GLOBALS['TL_DCA']['tl_my_extension']['fields']['my_tags'] = [
    'exclude'           => true
,   'inputType'         => 'select'
,   'foreignKey'        => 'tl_tags.tag'
,   'options_callback'  => ['numero2_tags.listener.data_container.tags', 'getTagOptions']
,   'load_callback'     => [['numero2_tags.listener.data_container.tags', 'loadTags']]
,   'save_callback'     => [['numero2_tags.listener.data_container.tags', 'saveTags']]
,   'eval'              => ['multiple'=>true, 'size'=>8, 'tl_class'=>'clr long tags', 'chosen'=>true]
,   'sql'               => "blob NULL"
,   'relation'          => ['type'=>'hasMany', 'load'=>'eager']
];

foreignKey, options_callbackand save_callbackare mandatory. In the eval section we add a class called tags - this is also needed for the JavaScript handling. There are some more options which can be defined in eval.

If you also want to enable copying tags while copying an entry, you must add following code to your dca

$GLOBALS['TL_DCA']['tl_my_extension']['config']['oncopy_callback'][] = ['numero2_tags.listener.data_container.tags', 'onCopy'];

This will copy all tags for the copied entry, except the fields excluded by doNotCopy.

Eval-Options

Option Description
groupTagsByField If set to true the user will only be able to select from a list of tags that have been already used on this specific field in the current table. If set to falseor not set at all, the user will be able to choose from all tags available.

Events

If you want to filter the tags shown in events and/or news, you can use the contao.tags_get_list event to modify the tags that will be used in the templates.

// src/EventListener/TagGetListListener.php
namespace App\EventListener;

use numero2\TagsBundle\Event\TagsEvents;
use numero2\TagsBundle\Event\TagsGetListEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener(TagsEvents::TAGS_GET_LIST)]
class TagGetListListener {

    public function __invoke( TagsGetListEvent $event ): void {
        // …
    }
}

numero2/contao-tags 适用场景与选型建议

numero2/contao-tags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.48k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「blog」 「events」 「tags」 「news」 「contao」 「tagcloud」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 numero2/contao-tags 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 numero2/contao-tags 我们能提供哪些服务?
定制开发 / 二次开发

基于 numero2/contao-tags 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.48k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

  • Stars: 2
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2021-09-30