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 ...
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 ...
5
votes
Accepted
What's the difference between a Component and a Model in Craft 3?
Models are classes that contains abstractions of the reality, for example you could have the model car that contains the technical information about it, how many wheels it has and it's brand. Models ...
4
votes
Accepted
Craft 3 plugin - how to update entry with field of Entries type
Ok this was easy. Tried it at first but didn't work and was successful after another go. All it needed was a array of Entry IDs.
Working code:
$entry = Entry::find()->id($entryId)->one();
$...
4
votes
Accepted
API - Update Asset Volume
You can move them like this
$asset = Asset::find()->volume('resourceImages')->filename($filename)->one()
$folder = Craft::$app->getAssets()->findFolder(['volumeId' => X]);
Craft::$...
4
votes
How can I tell if a variable is an array or an entry/element query?
you can use the instance of twig test here.
You want to flip your logic, so test for Query object first else array stuff but this should work I think.
To get the specific class just {% dd props....
3
votes
Hook into Entry::EVENT_AFTER_SAVE when user saves it
Your code is pretty close. You want to make sure it's a new entry and not a draft or a revision so you could use:
Event::on(
Entry::class,
Entry::EVENT_AFTER_SAVE, static function (ModelEvent $...
3
votes
Craft 3 plugin - how to update entry with field of Entries type
As per the docs at https://craftcms.com/docs/3.x/extend/element-types.html#saving-custom-field-values
When saving values on a custom field, always use the setFieldValue() or setFieldValues() methods ...
3
votes
Accepted
Add custom attribute to standard element from plugin
Another approach is to use the following event to attach a behavior
Event::on(Asset::class, Asset::EVENT_DEFINE_BEHAVIORS,
function(DefineBehaviorsEvent $event) {
$event->behaviors[] = ...
3
votes
Accepted
How can I add programmatically a Tag after SAVE_ON event in case of Assets types
A Tag field is like every other relation field. Just insert an array of ids in the field and save the element
$element->setFieldValue('fieldHandle', [1,2,3,4,5,6]);
Craft::$app->getElements()-&...
2
votes
Accepted
How to get related entry data using ElementCriteriaModel in a plugin
This is called Eager Loading you have to do
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->order('postDate asc');
$criteria->dateUpdated = array('and', '>='...
2
votes
Pass parameters to element query as array - in PHP
This is because it's a twig feature in Craft. If I remember correctly it's done in the CraftVariable and look something like this under the hood:
use Craft;
use craft\elements\Entry;
$query = Entry::...
2
votes
Pass parameters to element query as array - in PHP
The Element::find() method does not take any parameters, it just serves as the factory method for the appropriate element query for the given element type (for example, Entry::find() returns an ...
2
votes
How do we distinguish between a 'New' and 'Updated' Live Entry using the craft\elements\Entry::EVENT_AFTER_SAVE event?
Ben's historical explanation is 100% accurate, as is Brandon's technical explanation.
Here is a practical explanation...
Much of the time, you won't want this event triggered for Drafts and Revisions. ...
1
vote
Element API - lightswitch option to exclude an entry from a feed
For anyone else who happens to land on this trying to solve your similar challenge:
Super simple - the criteria can accept a custom field value, so in my case, the criteria became
'criteria' => [
...
1
vote
How to update stock on a product variant? Setting unknown property error
Have figured it out. Need to:
use craft\commerce\elements\Variant; // not Product
setFieldValue still does not find variable stock with Variant, but set it using $variantEntry->stock = 20;
Need to ...
1
vote
Users not fully deleting from the database
It definitely does sound like a missing foreign key constraint issue.
The Craft 2 version of the Fix FKs plugin for Craft 3 still lives here: https://github.com/pixelandtonic/craft-fixfks
I'd give ...
1
vote
How can I tell if a variable is an array or an entry/element query?
If you have a custom module/plugin, you can register a Twig extension, and then your class would look something like this:
<?php
namespace my\plugin\twigextensions;
use Twig\Extension\...
1
vote
Loop over table field in Element API plugin
A table field contains a simple array, you can serialize and unserialize it as it is. So there is no custom action required by your side.
Just use it in PHP like every other textual field
1
vote
Select specific fields from query
I tried eager loading before, and I couldn't figure out how to do it.
https://docs.craftcms.com/v3/dev/eager-loading-elements.html
Now I just query it using SQL. I know it's a bit hacking, but it ...
1
vote
Accepted
How html title is generated in entry index page in CP
it seems that first column of table view is special and is called here
which calls mentioned hook in question.
1
vote
Accepted
Array from matrix fields without quotations
You would need to join the array into a string, like so:
// Begin defining the response data for this entry
$data = [
'title' => $entry->title,
'slug' => $entry->url,
'status' ...
1
vote
Accepted
Matrix | Craft 3 Element API / Scout Plugin
The first code section looks correct, other than that you're first creating a bodyBlocks array and then assigning a string to it, but that's not really consequential and not the issue behind this ...
1
vote
Merge trashed element with existing one
https://github.com/craftcms/cms/pull/4404
You can now delete objects that implement ElementInterface using this method:
craft\services\Elements::mergeElements()
1
vote
Accepted
Custom element index page (cp) sortable on dateCreated?
The issue was in the modifyElementsQuery function. When you select all columns addSelect('*') to the query your SQL will select the dateCreated and dateUpdated columns of your custom table as well.
...
1
vote
Setting unknown property when trying to join table
You can't set a property of an element in Craft that does not exists in the field layout, as a behavior or as a public/private attribute in the object. Craft/Yii2 will throw an exception because the ...
1
vote
Accepted
How to build complex Element Criteria?
You can apply conditions within the same field but you can't create conditions for multiple fields.
Here are some great examples
$criteria->expiryDate = 'or, :empty:, =' . $yourDateValue;
...
1
vote
Anyway to extend Craft Entry query search in Craft 3
The "proper" way to access the element query that gets generated is through a plugin via the ElementQuery's query and subquery properties.
It looks like you're doing a Haversine algorithm to ...
1
vote
Using getCriteria with a custom element type
Fixed it for now by including a custom enum class in the base plugin file:
abstract class CustomElementType extends BaseEnum
{
const Badges_Badge = 'Badges_Badge';
}
and then querying for ...
1
vote
Eager Loading Image Transforms In Plugin
I came across this post while searching for the same answer. Figured it out eventually. For future reference, the correct syntax in a (Craft 2) plugin would be:
$criterea->with = [
["coolImage"...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
element × 26craft3 × 11
plugin-development × 5
entry × 4
elementcriteriamodel × 4
assets × 3
php × 2
plugin-elementapi × 2
events × 2
query × 2
elementquery × 2
eager-loading × 2
plugins × 1
users × 1
plugin-craftcommerce2 × 1
tags × 1
craft2 × 1
craft-pro × 1
array × 1
elementtype × 1
table-field × 1
models × 1
saveentry × 1
yii × 1
hook × 1