1

I want to utilise the Category field type but with the ability to choose from multiple category sources (as with Entries).

I noted in cms/src/fields/Categories.php that $this->allowMultipleSources is set to false. What is the simplest way of changing this to true without changing the source file?

Can I create a custom field that essentially just extends the original Category field but with $this->allowMultipleSources set to true?

2 Answers 2

1

Got there in the end and very simply through:

use Craft;
use craft\fields\BaseRelationField;
use craft\elements\Category;

class Categories extends BaseRelationField
{

    public static function displayName(): string
    {
        return Craft::t('app', 'Categories (multiple)');
    }

    protected static function elementType(): string
    {
        return Category::class;
    }

    public static function defaultSelectionLabel(): string
    {
        return Craft::t('app', 'Add a category');
    }
}

So simple

1

Isnt this possible with the modifyCategorySources hook?

-> https://docs.craftcms.com/v3/extend/updating-plugins.html#modifyentrysources-modifycategorysources-modifyassetsources-modifyusersources

I'm assuming you are using C3 by the namespace you mentioned. Correct me if this is wrong :).

2
  • Not entirely sure that is quite what am after. Want to create a new field type (rather than globally affect existing one) so that multiple category sources can be specified. Does that make sense? Commented Mar 28, 2018 at 15:47
  • Suppose really wondering whether you can extends BaseRelationField for your field as is the case with the existing Element Type fields Commented Mar 28, 2018 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.