Hot answers tagged

15 votes

Events calendar for Craft - which one should I use?

I haven't used Venti, so I can't comment on it. However, I have tried out both Calendars from Top Shelf Craft and Solspace Calendar on a recent project. I started out using Top Shelf Craft Calendars (...
user avatar
13 votes
Accepted

Bulk resave entries to trigger onSaveEntry event

If you re-save the section(s) that your entries are in, it should re-trigger a save of all of the entries. Hope that helps!
user avatar
  • 736
13 votes

Events documentation for Craft 3

Whilst Doug's answer is useful for knowing the ins and outs of a particular event once you've found it, actually finding which events are available in Craft 3 in the current docs is not straight-...
user avatar
  • 3,924
11 votes
Accepted

Booking paid-for events: How?

We did something similar with Charge when we were accepting payments for a Craft Virtual Training course. It does involve writing a plugin, but thankfully all of the heavy lifting for saving entries ...
user avatar
11 votes
Accepted

User events in Craft 3

There aren't specific User element save events in Craft 3, but you can listen to EVENT_AFTER_SAVE_ELEMENT and check if the element is a user like so: Event::on(\craft\services\Elements::class, \craft\...
user avatar
  • 66.4k
10 votes

How do we distinguish between a 'New' and 'Updated' Live Entry using the craft\elements\Entry::EVENT_AFTER_SAVE event?

If you want to run some code when changes are published for an entry, you’re going to want to check !ElementHelper::isDraftOrRevision($entry), which has been the case since Craft 3.2. Those saves only ...
user avatar
9 votes

Is there a way to detect if a action is triggered from a control panel form?

If anyone comes here looking for this answer for Craft 3 the function you are looking for is Craft::$app->getRequest()->getIsCpRequest()
user avatar
6 votes
Accepted

How do I extend another plugin?

I believe what you asking about are Hooks and Events. Craft has several of them listed in the Hooks Reference and Events Reference. And Craft also allows us to add Hooks and Events in our plugins, ...
user avatar
  • 13.2k
6 votes
Accepted

Have a custom plugin do something on every page load?

Add a init() method to your plugin and as long as it's installed and enabled, it'll be called on every request. public function init() { parent::init(); /* Do your stuff */ } If you want it ...
user avatar
  • 66.4k
6 votes
Accepted

Is there a way to detect if a action is triggered from a control panel form?

Yes there is, craft()->request->isCpRequest() returns whether the request is from the CP or not.
user avatar
  • 35.8k
6 votes

How to modify and save a field in entry on EVENT_AFTER_SAVE in a plugin?

The accepted answer works but it's for elements in general, of which an entry is just one. Instead, you can skip checking if the element is an entry (if ($event->element instanceof Entry)) and just ...
user avatar
  • 1,907
6 votes
Accepted

Events documentation for Craft 3

Here's a list of Event classes and the "Public Properties" that you can get or set: https://docs.craftcms.com/api/v3/craft-events-assetevent.html An example of getting event data: Event::on( ...
user avatar
6 votes

How do we distinguish between a 'New' and 'Updated' Live Entry using the craft\elements\Entry::EVENT_AFTER_SAVE event?

Brandon's answer was a helpful starting point for me but I think the answer goes deeper and has more scenarios to consider. Here's my attempt at clarifying that: Historic Context Before Drafts and ...
user avatar
  • 13.2k
5 votes
Accepted

Can a plugin fire a custom event?

Yes. It's not documented, but by instantiating the Event class (which is just a thin wrapper for Yii's CEvent) you can create and raise your own events. Obviously you could extend the Event class as ...
user avatar
5 votes

How do I stop an element from populating?

I am not really clear on what you are trying to do. My guess is you are trying to add your own validation to saving an element and trying to block that with your own custom checking of content values ...
user avatar
  • 6,672
5 votes

How do you raise/fake an event so that you can test handlers listening for it?

I believe events can be raised programmatically by calling them from services. You could mimic this behavior by creating an Event object ($event) and calling calling craft()->entries->...
user avatar
5 votes
Accepted

Programatically change a lightswitch value on an entry on save

You can set the value and save like so: craft()->on('entries.onSaveEntry', function(Event $event) { $entry = $event->params['entry']; if ($entry->section == 'mySection') { if ...
user avatar
5 votes
Accepted

Set execution order of plugins listening to same event

Not really – there's no priority system built into events for Craft or Yii. The easy way out is taking advantage of the fact that Craft actually loads plugins alphabetically, by name (i.e. the string ...
user avatar
5 votes

Events calendar for Craft - which one should I use?

I started using Solspace a couple weeks ago. The GUI is wonderful for content editors that will make use of it. ICS export so you could use it as the primary calendar manager and import the ICS into ...
user avatar
5 votes

What is the benefit of using a calendar event plugin or building your own?

Depending on your sites' actual event handling needs, building your own plugin similar to Solspace Calendar or Venti to handle this (specifically features like recurrences - the CP UI as well as ...
user avatar
  • 1,390
5 votes

EVENT_AFTER_SAVE_ELEMENT triggered twice after saving an entry

You can find out what the stack trace is (who is calling you) by putting something like this in your anonymous function: try { throw new \Exception(); ...
user avatar
5 votes
Accepted

EVENT_AFTER_SAVE_ELEMENT triggered twice after saving an entry

After using Andrew's debugging tip, I found out one event was triggered by saving the element itself, and the other was triggered by saving the revision of the element. So I modified my event listener ...
user avatar
  • 419
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
4 votes
Accepted

Entries where field = current month?

This should get it sorted. There were 2 issues with yours. First is you were using the wrong 'and' syntax in the craft.entries call and second you need to use the Y-m-d pattern so MySQL understands ...
user avatar
  • 66.4k
4 votes
Accepted

Calling an event from a third party plugin in my plugin

craft()->on('amForms_submissions.saveSubmission', function(Event $event) { BusinessLogicPlugin::log(print_r($event, true)); }); Pay attention to the casing Craft\AmForms_SubmissionsService → ...
user avatar
  • 35.8k
4 votes

Sprout Forms `onSaveEntry` event

Adding an event handler is not something you do through the control panel. What you need to do is create a plugin (pluginfactory.io makes this fairly easy), and in your plugin's main class, you have ...
user avatar
4 votes

Bulk resave entries to trigger onSaveEntry event

Craft can now do this via the CLI https://craftcms.com/docs/3.x/console-commands.html#resave-entries
user avatar
4 votes

User events in Craft 3

You can also try User::EVENT_BEFORE_SAVE. I'm not sure that it's documented anywhere, or if it was added after Brad responded, but it might work too. use yii\base\Event; use craft\elements\User use ...
user avatar
4 votes
Accepted

How to modify and save a field in entry on EVENT_AFTER_SAVE in a plugin?

Following @lenka suggestion, I was able to make the script work this way: use craft\services\Elements; use craft\events\ElementEvent; use craft\elements\Entry; Event::on( Elements::class, ...
user avatar
  • 173
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

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