heimrichhannot/contao-utils-bundle

本插件为 Contao CMS 提供了各种实用功能。

3.4.0 2024-04-11 13:53 UTC

This package is auto-updated.

Last update: 2024-09-13 08:30:02 UTC


README

example branch parameter Coverage Status

您好,您正在查看 utils 插件的一个非常新的版本,版本 3!有关更多信息,请参阅 CHANGELOG.md。如果您在寻找版本 2,请检查 v2 分支

Utils Bundle 是一系列小型辅助工具的集合,用于解决重复任务。核心是一个 utils 服务,允许访问所有实用函数。此外,还有 DcaField 辅助工具、实体查找命令和一些不错的 twig 过滤器。

功能

  • Utils-Service - 一个服务,允许访问所有捆绑的实用函数。
  • DcaField 注册 - 一个很好的 API,可以将典型 dca 字段添加到您的 dca 字段中,而无需重复自己或令人烦恼的顺序限制。
    • AuthorField - 添加一个作者字段,具有自动填充默认值和可选的前端成员支持
    • DateAddedField - 添加一个日期添加字段以存储实体创建的日期
  • Entity Finder - 一个命令,用于在您的数据库中搜索任何 Contao 实体。
  • Twig 过滤器

安装

只需通过 composer 或 Contao 管理器安装即可

composer require heimrichhannot/contao-utils-bundle

用法

Utils 服务

Utils 服务是此插件的核心功能。它提供了对大量帮助解决重复任务的实用函数的访问。它构建为一个服务,您可以通过该服务访问所有 utils 服务。Utils 服务最好与依赖注入一起使用,但也可以从服务容器作为公共服务用于旧代码。您可以查看 API 文档 以查看所有可用函数。

use HeimrichHannot\UtilsBundle\Util\Utils;

/**
 * A class containing examples usage of utils services. Please don't expect it to be useful :)
 */
class MyClass{
   /** @var Utils */
   protected $utils;
    
   public function __construct(Utils $utils) {
       $this->utils = $utils;
   }
   
   public function someActions(): bool {
        $dcaFields = $this->utils->dca()->getDcaFields('tl_content');
        $this->utils->array()->removeValue('headline', $dcaFields);
        foreach ($dcaFields as $dcaField) {
            echo $this->utils->string()->camelCaseToDashed($dcaField);
        }
        
        $rootPageModel = $this->utils->request()->getCurrentRootPageModel();
        echo $this->utils->anonymize()->anonymizeEmail($rootPageModel->adminEmail);
        
        $groupUsers = $this->utils->user()->findActiveUsersByGroup([1,2]);
        $this->utils->url()->addQueryStringParameterToUrl('user='.$groupUsers[0]->username, 'https://example.org');
        
        if ($this->utils->container()->isBackend()) {
            $where = $this->utils->database()->createWhereForSerializedBlob('dumbData', ['foo', 'bar']);
            $model = $this->utils->model()->findOneModelInstanceBy('tl_content', [$where->createAndWhere()], [$where->values]);
            echo '<div '.$this->utils->html()->generateAttributeString($model->getHtmlAttributes()).'></div>';
        }
}

静态 Utils

SUtils 定位器提供了不需要注入的静态辅助方法。

目前,以下静态辅助方法可用

use HeimrichHannot\UtilsBundle\StaticUtil\SUtils;

SUtils::array()->insertBeforeKey($array, $keys, $newKey, $newValue);
SUtils::array()->insertAfterKey($array, $key, $value, $newKey = null, $options = []);
$foundAndRemoved = SUtils::array()->removeValue($value, $array);

SUtils::class()->hasTrait($class, $trait);

Dca 字段

此插件提供了一些常见的 dca 字段,可以在您的 dca 文件中使用。

作者字段

将作者字段添加到您的 dca。它将使用当前后端用户进行初始化。在复制时,它将设置为当前用户。

# contao/dca/tl_example.php
use HeimrichHannot\UtilsBundle\Dca\AuthorField;

AuthorField::register('tl_example');

您可以通过传递额外的选项来调整字段

# contao/dca/tl_example.php
use HeimrichHannot\UtilsBundle\Dca\AuthorField;

AuthorField::register('tl_example')
    ->setType(AuthorField::TYPE_MEMBER) // can be one of TYPE_USER (default) or TYPE_MEMBER. Use TYPE_MEMBER to set a frontend member instead of a backend user
    ->setFieldNamePrefix('example') // custom prefix for the field name
    ->setUseDefaultLabel(false) // set to false to disable the default label and set a custom label in your dca translations
    ->setExclude(false) // set the dca field exclude option
    ->setFilter(false) // set the dca field filter option
    ->setSearch(false) // set the dca field search option
    ->setSorting(false) // set the dca field sorting option
;

日期添加字段

将日期添加字段添加到您的 dca。它在创建或复制时将设置时间戳。

# contao/dca/tl_example.php
use HeimrichHannot\UtilsBundle\Dca\DateAddedField;

DateAddedField::register('tl_example');

您可以通过传递额外的选项来调整字段

# contao/dca/tl_example.php
use HeimrichHannot\UtilsBundle\Dca\DateAddedField;

DateAddedField::register('tl_example')
    ->setExclude(false) // set the dca field exclude option
    ->setFilter(false) // set the dca field filter option
    ->setSearch(false) // set the dca field search option
    ->setSorting(true) // set the dca field sorting option
;

实体查找器

实体查找器是一个命令,用于在您的数据库中搜索任何 Contao 实体。

实体查找器

Twig 过滤器

此插件目前包含一个 twig 过滤器

anonymize_email

返回一个匿名化电子邮件地址。 max.muster@example.org 将是 max.****@example.org

{{ user.email|anonymize_email }}

备注

向后兼容性承诺

我们尽力保持此插件向后兼容,并遵循 语义版本控制 原则。

以下方面不受 BC 承诺的涵盖

  • 直接从 Utils 服务使用 Utils 类。这不受官方支持,并且可能会因内部更改而破坏您的应用程序。
  • 标记为 @internal@experimental 的类