Skip to main content
Share Your Experience: Take the 2024 Developer Survey
9 votes
Accepted

$allowAnonymous must be array|int|bool

Your module was created for Craft 3, but you're using Craft 4. Make sure to follow the upgrade guide and the plugin update guide if you're updating a site with custom modules to Craft 4. You're seeing ...
MoritzLost's user avatar
  • 11.4k
6 votes
Accepted

Running module migrations

Per the Modules vs. Plugins docs, only plugins get their own migration track. Modules are considered part of the project (like templates and config files), so if there are any DB changes that need to ...
Brandon Kelly's user avatar
6 votes
Accepted

How to render a template from within a module?

You need to include your template roots Enhancing a Craft CMS 3 Website with a Custom Module public function __construct($id, $parent = null, array $config = []) { Craft::setAlias('@modules/...
Robin Schambach's user avatar
6 votes
Accepted

How can I install modules with Composer?

Craft Plugins require that a "type":"craft-plugin" property is defined in each composer.json file. Composer refers to this to help manage and install the plugin. Craft Modules do not currently have ...
Mark Notton's user avatar
  • 2,337
6 votes
Accepted

Create a pending user from plugin

Rolling with this does what I need $newUser = new User([ 'firstName' => $submission->firstName->getValue(), 'lastName' => $submission->lastName->getValue(), 'username' => $...
Steven Grant's user avatar
  • 1,855
5 votes
Accepted

Return the contents of a template from a custom Twig function in a module?

There is no craft() function in Craft 3 and there are no Modules in Craft 2. You probably mix the two different versions. Craft 3 Craft::$app->getView()->renderTemplate('/path/to/template'); Of ...
Robin Schambach's user avatar
5 votes

How to set field values for the table field type in Craft 3

I figured this out. The table field is expecting JSON: $entry = new Entry(); $entry->sectionId = 1; $entry->typeId = 1; $entry->title = $title; $entry->setFieldValues([ "field_handle" =&...
David Jones's user avatar
5 votes
Accepted

Is there a way to compare what has changed on an entry while it's being saved?

You're probably looking for Element::getDirtyAttributes() and Element::getDirtyFields()! The former is for native class properties/attributes (and is based on an ActiveRecord pattern in Yii, I believe....
August Miller's user avatar
5 votes
Accepted

How to loop through specific entries in plugin and edit and save

You can use entry queries to fetch the entries and the elements service to save them. You can access the element service using Craft::$app->elements. Use Craft::$app->elements->saveElement($...
MoritzLost's user avatar
  • 11.4k
4 votes

How to develop plugin/module to build landingpage upon fetched JSON-data

I'd make a template called landing that would have various entries/entry types in it where I can add whatever CMS content I want. https://www.example.com/landing/landing-page-slug For a nice content ...
andrew.welch's user avatar
  • 11.6k
4 votes

How to call Controller in Module?

You need to include your module handle to the route module-handle/site/say You also should use the correct namespace instead of app/controllers I suggest you to read the documentation You can as ...
Robin Schambach's user avatar
4 votes

Basic Module Tutorial or Example

I haven't tried this tutorial yet, but having learned a lot about Craft 3 from Andrew Welch's other tutorials, I don't doubt this may have some nuggets of useful information to get you on your way. ...
Jay's user avatar
  • 1,042
4 votes
Accepted

Craft 3 Custom Field Type with a File Uploader

Just as a little example how to store files in a custom field /** * @inheritdoc */ public function normalizeValue($value, ElementInterface $element = null) { $namespace = $element? $element-&...
Robin Schambach's user avatar
4 votes

How to change an entry's slug on EVENT_AFTER_SAVE_ELEMENT?

You can change the value in the DB directly instead of using the Elements service $entry = $event->element; $elementId = $entry->id; $siteId = $entry->siteId; Craft::$app->getDb()->...
Robin Schambach's user avatar
4 votes
Accepted

Modules - CP Template not found

It must be /admin/module-id/leads. So in your case maybe /admin/leadsmodule/leads or /admin/leads-module/leads depending on your ID if you want to access it directly via template route. Otherwhise ...
Robin Schambach's user avatar
4 votes
Accepted

Use sidebar nav icons in modules and plugins

You were almost there with it. You need to specify a path to an SVG. It can be one of the ones that exists within Craft's icon directory. So your nav item would look like this: Event::on( Cp::...
nfourtythree's user avatar
4 votes
Accepted

Trouble finding the correct path to template in custom module and getting the "Unable to find the template" error

If this is a module and not a plugin, you might need to declare your base template directory in the Module's __construct using: Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function ...
Oli's user avatar
  • 7,495
4 votes
Accepted

How do I get the siteName in a module (Craft 3)

When searching the Craft 3.5.x docs for siteName, I see references to Global Variables and General Config Settings. Screenshot: https://cln.sh/egvIXhxZSzaFjeCgNLxX However, AFAICT, it works ...
Andrea DeMers's user avatar
4 votes

What is the correct way to format a module controller action URL?

It depends on the module handle you chose in config/app.php (the module handle is the key affected to your module in the modules array). If you want the URL to be /business-logic/example/example-route,...
nstCactus's user avatar
  • 1,149
4 votes
Accepted

What is the correct way to format a module controller action URL?

The cause of the issue turned out to be an incorrect controllerNamespace reference in the Business Logic Template example. If anyone else is using the example project to jumpstart a Craft module, be ...
Dan Madoni's user avatar
4 votes

How to make custom element types restorable?

You have 2 solutions: Implement your own Restore action, or Be lazy and reuse \craft\elements\actions I won't cover implementing you own action, let's be lazy! Declare the action To do this, you ...
nstCactus's user avatar
  • 1,149
4 votes
Accepted

Custom validation & messages

The User class already defines a password rule on the newPassword attribute. $rules[] = [ ['newPassword'], UserPasswordValidator::class, 'forceDifferent' =&...
Ben Croker's user avatar
  • 7,526
3 votes
Accepted

How can I set new user accounts to pending status in a Craft 3 module?

Yeah you’re correct that db089251 is the culprit here, which fixed a bug where user activation events weren’t getting fired when users were first created, if the “Verify email addresses?” setting was ...
Brandon Kelly's user avatar
3 votes
Accepted

Module: EVENT_AFTER_SAVE_ELEMENT won't detect changed user group

That's because groups are not attached to the user directly. At the moment Craft stores the user it hasn't handled the permissions not the user groups. The workflow is Handling all kinds of ...
Robin Schambach's user avatar
3 votes
Accepted

Demo repo for an Adjuster module for Commerce 2?

I've created a stub/reference Adjuster module, here. This demonstrates how to register two Adjusters via a custom module—one that creates a 5% discount on each line item, and one that creates a 10% ...
August Miller's user avatar
3 votes
Accepted

How do I delete an asset with module or plugin

I'm not sure how well it'd go with 50,000 Assets, but you should just be able to call Craft::$app->elements->deleteElement($asset) for each one, or Craft::$app->elements->deleteElementById(...
Matt Stein's user avatar
  • 4,006
3 votes

Basic Module Tutorial or Example

These are the pieces I needed to complete the puzzle: Docs for Craft controllers Docs for Yii controllers Create a FooController class in your module Add a actionBar() method in that class Make sure ...
Tamlyn's user avatar
  • 183
3 votes
Accepted

"Template Not Found" error when trying to access a module controller action

You'll either need to use the action url http://localhost/actions/my-module/scrape/push since only those will route to controllers by default or create a custom route to your controller
Robin Schambach's user avatar
3 votes

How do I load composer dependencies for a Yii module that's in my site repo?

With a little nudge from Brad, here's the answer: In order to get the module's composer dependencies taken into account in the parent site's composer dependencies, you need to add the module in the ...
Jeremy Gimbel's user avatar
3 votes

Simple Module in Craft CMS 3

Start with the manual: https://docs.craftcms.com/v3/extend/module-guide.html This blog post covers module development in more detail: https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-...
Richard's user avatar
  • 181

Only top scored, non community-wiki answers of a minimum length are eligible