Hot answers tagged

5 votes
Accepted

Maintaining order when getting by entries by ID

You can do this by adding .fixedOrder(true) to your query: {% set ids = ['4213','3200','4204','4212'].fixedOrder(true) %} https://docs.craftcms.com/v3/dev/element-queries/tag-queries.html#fixedorder
user avatar
  • 3,594
4 votes
Accepted

Sending a custom email in the users prefered locale

You can translate string in PHP and Twig via translate function public static function t($message, $variables = array(), $source = null, $language = null, $category = 'craft') Twig {{ 'my string'|t(...
user avatar
4 votes

Merging 2 Entries Array's

Just a few side notes: neither the first entries variable nor the second entries variable is an array - both are objects of kind craft\elements\db\EntryQuery (or craft\ElementCriteriaModel if it's ...
user avatar
4 votes
Accepted

How can I return only 1 random block in a Matrix in Craft using Twig?

You can create a random order by including rand() in your OrderBy function. {% set randomBlock = entry.carouselMatrix.orderBy('rand()').one() %} {% if randomBlock is not null %} {% set ...
user avatar
4 votes
Accepted

Local DB SQL 'CDbException' Errors on Craft 2 site

this is a MySQL/i version issue on Craft 2 sites. You'll need to add this to your database config (craft/config/db.php): 'initSQLs' => array("SET SESSION sql_mode='STRICT_TRANS_TABLES,...
user avatar
  • 1,721
4 votes

Just upgraded to Craft 3, environmentVariables changed or stopped working

Environment variables don't exist in Craft 3 in the same way that they existed in Craft 2. Instead, they have been replaced with system environment variables and aliases. The Changes in Craft 3 docs ...
user avatar
3 votes
Accepted

How to directly get value of text field in a given section of type structure?

Try this: {% set value = craft.entries.section('mySectionHandle').type('entryTypeHandle').first().myTextFieldHandle %} {{ value }} Just with your field handle at the end
user avatar
3 votes
Accepted

Ajax Post to Plugin Controller

Based on your controller's class name, I believe you need to POST to this endpoint... /actions/googleMap/map/getLocations
user avatar
  • 23.6k
3 votes

How to have Craft 3 and 2 on same host?

Both the craft/ folder name in Craft 2, and the craft executable in Craft 3, can be renamed. Since the craft executable is user-facing (when SSH’d in), I’d recommend leaving that alone and rename ...
user avatar
3 votes

How to create an entry with MySQL?

If you really, really want to create an entry from within the database (you must have a VERY good reason to do so), then you should create a row in: entries elements elements_sites content If you'd ...
user avatar
  • 6,288
3 votes
Accepted

German special characters like umlauts (ä, ö, ü) are not converted correctly with convertFilenamesToAscii

This was a bug and has now been fixed in Craft 3.5.12.1 https://github.com/craftcms/cms/issues/6923
user avatar
  • 129
3 votes

mkdir(): Permission denied

It's been a while since I was in Craft 2, but I'd suggest checking out this page in the docs: https://craftcms.com/docs/2.x/folder-structure.html Do all of the directories mentioned on that page exist ...
user avatar
2 votes

Programmatically Login From Plugin

If you want to login a user, you need to use the login function craft()->userSession->login($loginName, $password, $rememberMe))
user avatar
2 votes
Accepted

Impossible to invoke a method ("limit") on an array

craft.entries now returns an ElementQuery. When you add the .all() you're telling Craft to execute the query, which returns an array of Elements. That means that solutions is now a simple array, and ...
user avatar
2 votes
Accepted

Custom plugin log not logging

Put the force parameter to true MyPlugin::log("Errors:\n\n" . print_r($fieldsData, true), LogLevel::Error, true); Custom Log in Craft
user avatar
  • 36
2 votes
Accepted

Is it possible to pass a variable to an event in a plugin?

This will not work as the field value will be the new value in both events. You'll need to get the old value from the entry that exists in Craft before saving the new entry, and then perform the ...
user avatar
  • 6,226
2 votes
Accepted

Adding entry type using setContentFromPost

Relationships are always added as arrays of IDs, even if you are only relating a single element: $user->setContentFromPost(array( 'companyName' => $admin->companyName, 'companyId' =&...
user avatar
  • 4,361
2 votes

Failed Stale Template Cache Delete Error...Analyzed...But No Answer

OK, I found the answer to this. Here is the correct answer. Basically change dateCreated to elements.dateCreated in my template.
user avatar
  • 283
2 votes
Accepted

Variable "entry" does not exist in template at Structure Section

That entry variable will only be predefined for the template when you go to one of your entry’s URLs. Your section’s Entry URL Format for top-level entries is {slug}, and your entry’s slug is ...
user avatar
2 votes
Accepted

Multiple and or relatedTo in plugin element criteria

Aren't you missing the ->find() on the last bit? $entries = craft()->elements->getCriteria(ElementType::Entry, [ 'section' => 'products', 'relatedTo' => ['and'...
user avatar
  • 7,160
2 votes
Accepted

Update a lightswitch field in Craft CMS 2

If it's only one single field and you want to bypass all Craft validations for required fields you can change the value directly in your database using Yii commands and the update function function ...
user avatar
2 votes

CSRF token is null or empty in craft 2

For Craft 2, these are the values you're looking for... window.csrfTokenName = "{{ craft.config.get('csrfTokenName') }}"; window.csrfTokenValue = "{{ craft.request.getCsrfToken }}"; They changed ...
user avatar
  • 23.6k
2 votes

Clear template cache after update sale on Craft Commerce

More than likely the issue is not the prices not updating, but the sale conditions as Luke mentioned. In theory, Craft will automatically clear template caches when an element within the cached ...
user avatar
2 votes
Accepted

Pagination Slug Issue

Pagination is for breaking up a list of entries into different pages. p3 means page 3 of entries. What you're looking for is the ability to link to the next/previous entry. If you are ordering your ...
user avatar
  • 2,985
2 votes
Accepted

Craft 2 Commerce 1 cart session expiry

Create a commerce.php config file in your config folder and set the cartCookieDuration to a time interval. The default is 3 months. <?php return [ 'cartCookieDuration' => 'P3M', '...
user avatar
  • 6,682
2 votes

Which twig file is being used and how?

You could use this variable to get the template name: {{ _self.getTemplateName() }} Put it at the top of all your template files: <!-- {{ _self.getTemplateName() }} --> And then for every ...
user avatar
  • 482
2 votes
Accepted

Hide flagged (inappropriate) comments from comment list

You can use the isFlagged() method to only display non-flagged comments: {% set comments = craft.comments.all({ limit: 10, status: 'approved' }) %} {% for comment in comments if comment....
user avatar
2 votes
Accepted

How to filter entries with null value in date field?

The special strings :empty: and :notempty: might be what you're looking for. The docs list it only against expiryDate, but I think it's available for any field: https://docs.craftcms.com/v3/dev/...
user avatar
  • 3,924
2 votes
Accepted

Send email notification to user's email when their entry has been enabled

You could build a small plugin to hook into Craft's onBeforeSaveEntry event (docs). Before the entry is saved (and enabled), you'd check if it was previously disabled and its new status is live, if it ...
user avatar
  • 7,160
2 votes

Admin panel not working without language code

Solved, I left an .htaccess file in root. It wasn't in local server neither, but It seems it was some config in virtual host or something like that to solve it. Once I find this .htaccess in other ...
user avatar

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