Hot answers tagged

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
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

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 ...
user avatar
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(); $...
user avatar
  • 621
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::$...
user avatar
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....
user avatar
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 ...
user avatar
  • 6,146
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[] = ...
user avatar
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()-&...
user avatar
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', '>='...
user avatar
2 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 $...
user avatar
  • 7,095
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::...
user avatar
  • 21
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 ...
user avatar
  • 5,995
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. ...
user avatar
  • 23.6k
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' => [ ...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 66.4k
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\...
user avatar
  • 1,887
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
user avatar
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 ...
user avatar
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.
user avatar
  • 467
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' ...
user avatar
  • 608
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 ...
user avatar
  • 608
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()
user avatar
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. ...
user avatar
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 ...
user avatar
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; ...
user avatar
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 ...
user avatar
  • 66.4k
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 ...
user avatar
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"...
user avatar

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