10
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 ...
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 ...
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/...
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 ...
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' => $...
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 ...
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" =&...
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....
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($...
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 ...
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 ...
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.
...
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-&...
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()->...
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 ...
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::...
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 ...
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 ...
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,...
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 ...
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 ...
4
votes
Accepted
Custom validation & messages
The User class already defines a password rule on the newPassword attribute.
$rules[] = [
['newPassword'],
UserPasswordValidator::class,
'forceDifferent' =&...
4
votes
How to create Twig extension in Craft CMS 5
There's a page in the docs you might have missed that covers this here:
https://craftcms.com/docs/5.x/extend/extending-twig.html
So specifically, you want to bind the EVENT_INIT event of the Craft ...
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 ...
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 ...
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% ...
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(...
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 ...
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
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
modules × 199craft3 × 57
plugin-development × 41
controller × 18
events × 16
templating × 15
php × 11
users × 10
assets × 8
plugins × 8
plugin-craftcommerce × 7
control-panel × 7
plugin-craftcommerce2 × 7
composer × 6
entries × 5
matrix × 5
emails × 5
custom-field × 5
field × 4
plugin-feedme × 4
plugin-craftcommerce3 × 4
table-field × 4
yii × 4
twigextension × 4
craft4 × 4