Hot answers tagged

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 ...
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 ...
user avatar
  • 2,265
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 ...
user avatar
5 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/...
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....
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($...
user avatar
  • 6,350
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. ...
user avatar
  • 1,032
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 ...
user avatar
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 ...
user avatar
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-&...
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()->...
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 ...
user avatar
4 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" =&...
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::...
user avatar
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 ...
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,...
user avatar
  • 1,130
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 ...
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 ...
user avatar
  • 1,130
4 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' => $...
user avatar
  • 1,793
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 ...
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 ...
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% ...
user avatar
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
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 ...
user avatar
3 votes
Accepted

Trouble parsing JSON with getBodyParams()

Got in touch with P&T support and got the following response back: We don't currently support parsing JSON via getBodyParams(), however I'm looking into it and looks like Yii can be configured ...
user avatar
  • 127
3 votes

Trouble parsing JSON with getBodyParams()

As of Craft 3.3, if you send a request to Craft with a Content-Type: application/json header, Request::getBodyParam('property') will work as expected, returning the property value from the JSON data ...
user avatar
3 votes

PS-4 Autloading custom module

I don't think you need to add any composer.json in your module folders You have to update the project composer.json file only to let the craft know about custom module so that it can load on each boot....
user avatar
3 votes
Accepted

Restrict Sources on element select field

I am not expert in Craft CMS but this is how it works for me: Depending on the element type, source is different. Look at this link how Craft CMS implements this: https://github.com/craftcms/cms/...
user avatar
  • 467
3 votes
Accepted

Can you call a controller method from within a plugin/module in Craft 3?

Based on your goal, I don't recommend taking this approach for the technical debt reasons in my comment, however your actual question is still a valid one. You can call any controller's method from a ...
user avatar
  • 3,924
3 votes

How to reliably determine if an entry is being duplicated?

If you don't mind me rearranging your code a bit, I might go with something more like this... Event::on(Entry::class, Entry::EVENT_BEFORE_SAVE, static function (ModelEvent $event) { /** @var ...
user avatar
  • 23.6k

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