Hot answers tagged

6

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 this support. You can see all package types that Composer Installer supports here. There is still a way to achieve what you're trying to do though... Composer ...


5

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...), and the latter is for Craft's "fields," which may or may not be available, based on what type of Element you're looking at (and what FieldLayout ...


4

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. https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module


4

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 be made for the module to run, those changes should be made via content migrations. If the module is meant to be reused across multiple projects and does ...


4

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 authoring experience, I'd make a custom Element that knows to go out over the wire and fetch the data from the Laravel database via whatever JSON endpoint you'...


4

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 course you need to be in the correct namespace. You have to either use Craft or do \Craft Craft 2 craft()->templates->render('path/to/template')


4

Just as a little example how to store files in a custom field /** * @inheritdoc */ public function normalizeValue($value, ElementInterface $element = null) { $namespace = $element? $element->getFieldParamNamespace() . '.' : ''; $file = UploadedFile::getInstanceByName($namespace . $this->handle); if($file !== null){ // replace ...


4

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()->createCommand() ->update( '{{%elements_sites}}', ['slug' => ElementHelper::createSlug('new-slug')], [ 'elementId' =&...


4

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::class, Cp::EVENT_REGISTER_CP_NAV_ITEMS, function(RegisterCpNavItemsEvent $event) { $event->navItems[] = [ 'url' => 'contact-...


4

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 differently for modules/plugins. As per the Sites section in the Craft 3.5.x Class Reference docs at https://docs.craftcms.com/api/v3/craft-services-sites.html, you would ...


4

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, the module key must be business-logic: use modules\businesslogic\BusinessLogic; return [ 'modules' => [ 'business-logic' => BusinessLogic::...


3

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

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 disabled. Your new code is close, but that is going to get called each time Craft tries to activate a user—both when they are first created, and when you try ...


3

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% discount on the entire order. These adjusters have their logic hard-coded, which is to say: in order to modify the behavior/value of the adjustments, you have ...


3

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 parent's composer.json, both as a require and as a repository with the path type, like so: { ... a bunch of stuff from the default composer.json goes here ... ...


3

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 well define a route if you like


3

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 to support that, when the request's Content-Type header is set to application/json. We would need to enable that feature ourselves though. In the meantime ...


3

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/blob/60881865f1b680c5aa5d4ad8f56d9a1ce3e452b0/src/fields/BaseRelationField.php#L842 Now how assets implement inputsource function: https://github.com/craftcms/...


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 plugin/module like this: Craft::$app->runAction('users/save-user');


2

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/sitemodule', $this->getBasePath()); $this->controllerNamespace = 'modules\sitemodule\controllers'; // Base template directory Event::on(View::...


2

Assuming what you're trying to attach is on the local filesystem, you can see an example of this here: https://github.com/pixelandtonic/craftnet/blob/master/src/orders/OrderBehavior.php#L160-L182 Essentially, you'd just use attachContent, but there is also an attach method: ->attachContent($pdf, [ 'fileName' => $filename, 'contentType' => '...


2

In the context of Craft CMS, Modules typically would be used for adding functionality to the website itself. As such, you'd use it for things that are specific to that project. Check out the article Enhancing a Craft CMS 3 Website with a Custom Module for details. You can also generate your module scaffolding via pluginfactory.io


2

It's a bit difficult without seeing all of your code, but I'm going to guess that you also need to make changes in your config/app.php to make the namespace changes you're making. So two things I'd recommend... first, you can generate your module's framework here: https://pluginfactory.io/ ...and second I wrote up a tutorial on creating a module in the ...


2

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 the $this->controllerNamespace in your module init is correct Now, assuming your module is registered as baz-module in config/app.php then you can reach your ...


2

That's because those buttons are ActionButtons and they have actually nothing to do with statuses in general since the status is not a single field value nor is there a value in the DB that's called status. For example: the status of an entry depends on several different attributes like expiryDate or the date when it's published so there is no way to set ...


2

I think this can be a starting solution. It rewrite slug of Entry after the Entry is saved to database. The URI is constructed from it. You can try it in any plugin, only register the EVENT_AFTER_SAVE_ELEMENT in init() function of Plugin. use craft\events\ElementEvent; use craft\services\Elements; Event::on( Elements::class, Elements::...


2

Was running into the same problem today. I was able to access the controller and the service, but none of the variables. Looks like Brad was able to fix the problem but didn't add a final solution. I wasn't registering any variables on the init() function. Adding the last line of code registered the variable but did not load it onto every request. // ...


2

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($asset->id), as seen in the Elements Class Reference.


2

I figured this one out, it was bad error handling on my part. If the service you're calling returns NULL, this will appear in the network tab as a 404.


2

This isn't something that is built-in, probably the cleanest way would be to write a plugin that adds a Utility that lists your installed modules. You can see all of the installed modules via: Craft::$app->modules ...but keep in mind that this will include all of your plugins as well, since they are modules, too. You can exclude them by doing something ...


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