Hot answers tagged

5

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 are usually not supposed to contain any logic. So it's just a "container" with information Components are more like services that contains business logic to ...


3

You can move them like this $asset = Asset::find()->volume('resourceImages')->filename($filename)->one() $folder = Craft::$app->getAssets()->findFolder(['volumeId' => X]); Craft::$app->getAssets()->moveAsset($asset, $folder, $filename); It won't change your relations or anything else in fact all the function does is: /** * Move or ...


3

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[] = MyBehavior::class; }); The behavior could look like class MyBehavior extends Behavior { public $downloads; } This way you won't receive an exception and can ...


3

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()->saveElement($element); In order to get the current ids and add one, just do $ids = $element->getFieldValue('fieldHandle')->ids(); $ids[] = '...


2

This is called Eager Loading you have to do $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->order('postDate asc'); $criteria->dateUpdated = array('and', '>=' . $this->dateFrom->format(DateTime::MYSQL_DATETIME), '<=' . $this->dateTo->format(DateTime::MYSQL_DATETIME)); $criteria->limit('null'); // =...


2

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(); $entry->textField = 20; // works $entry->entriesField = [$relatedEntryId1, $relatedEntryId2]; // works Craft::$app->elements->saveElement($entry);


1

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

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 gets the job done. The DB structure of craft_content table hasn't change since Craft 2 (when I started using craft), so its unlikely that future updates will ...


1

it seems that first column of table view is special and is called here which calls mentioned hook in question.


1

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' => $entry->status, 'textArea' => implode(' ',$bodyBlocks) ];


1

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 failing. Do yourself a favor and set up PHPStorm + xdebug, you'll find the issue very easily. I suspect you might have some mixed up identifiers (field names, ...


1

https://github.com/craftcms/cms/pull/4404 You can now delete objects that implement ElementInterface using this method: craft\services\Elements::mergeElements()


1

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. As soon as you want to order by one of those columns the database does not know which dateCreated should be sorted. You need to select only the unique attribute ...


1

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 property instance_datetime probably does not exist. In order to attach a custom value you have to either create a new element or you have to store this ...


1

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; However you can't combine conditions for multiple fields. So you can't say Where (fieldX = 4 OR FieldY = 5) you would have to create an sql object based on your ...


1

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 calculate distance between a latitude and longitude. There's are some similar plugins that already do this you can use as an example. For example: https://github.com/...


1

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 the custom element in the plugin using craft()->elements->getCriteria(CustomElementType::Badges_Badge);


1

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", ["withTransforms" => ["transform"]]], "otherAssetField", "someCategoryField" ];


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