Hot answers tagged

4

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 Craft 2). .search('nonMembers:0') can be shortened to nonMembers(0) (or "0" or "not 1" depending on the type) and search('membersExclusive:1') to ...


4

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 randomSlide = randomBlock.slides.orderBy('rand()').one() %} {% if randomSlide is not null %} {{ randomSlide .caption }} {% set image = randomSlide....


4

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 explain what changes have been made and the Environmental Configuration explains how you can set these up and use them.


3

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


3

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() }} PHP Craft::t('my string'); So all you need to do is to wrap every string in your templates within the translate filter and pass the preferred local as ...


3

Based on your controller's class name, I believe you need to POST to this endpoint... /actions/googleMap/map/getLocations


3

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


3

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 your craft/ folder to something else, maybe craft2/. Then in public_html/index.php, update your $craftPath variable to point to the new location: $craftPath = '../...


3

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 like the easier path, use Content migrations. Create a migration, and then add something like: // Create the entry $myNewEntry = new craft\elements\Entry(); $...


2

OK, I found the answer to this. Here is the correct answer. Basically change dateCreated to elements.dateCreated in my template.


2

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 won't have methods like .limit(1) that can be executed on it. It looks like you just want to use the first item in the solutions array, so try changing this: ...


2

Put the force parameter to true MyPlugin::log("Errors:\n\n" . print_r($fieldsData, true), LogLevel::Error, true); Custom Log in Craft


2

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 comparison. If the value has changed then store the entry ID to process once the entry has successfully saved. Note that the $entryIdChanged variable in the ...


2

Relationships are always added as arrays of IDs, even if you are only relating a single element: $user->setContentFromPost(array( 'companyName' => $admin->companyName, 'companyId' => $admin->companyId, 'partnerManager' => array($admin->partnerManager[0]->id) ));


2

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 qweasdzxc, so that would make your entry’s URL <site-url>/qweasdzxc (http://localhost/qweasdzxc). If you change your section’s top-level Entry URL Format to ...


2

Aren't you missing the ->find() on the last bit? $entries = craft()->elements->getCriteria(ElementType::Entry, [ 'section' => 'products', 'relatedTo' => ['and', $fabric_categories, $color_categories], ])->find(); return $entries;


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 update($table, $columns, $conditions='', $params=array()) $command = craft()->db->createCommand(); $command->update('craft_content', array( ...


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 slightly in Craft 3, which can be seen here...


2

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 region is updated (note one trap is this process of deleting stale template caches runs as a job task, so may not happen absolutely immediately...give things 30 ...


2

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 entries like a blog, by post date, newest first you can create the links like this. On your single entry template: {% set nextEntry = craft.entries.section('...


2

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', 'purgeInactiveCarts' => true, 'purgeInactiveCartsDuration' => 'P3M', ];


2

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,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';") As per discussion at https://craftcms.stackexchange.com/a/12106/992


2

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 template called it should have the commented out template name. In Craft 3, there's a nice plugin called "Template Comments" which is great for this... But I don'...


2

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.isFlagged() == false %} {{ comment.comment }} {% endfor %}


2

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/element-queries/entry-queries.html#expirydate


2

This was a bug and has now been fixed in Craft 3.5.12.1 https://github.com/craftcms/cms/issues/6923


2

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 is, send an email. The code below should get you going. Save it as NotifyPlugin.php in a craft/plugins/notify folder. <?php namespace Craft; class ...


2

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 craft project, was solved ;) Don't forget your .htaccess in root!


1

Ended up getting this resolved, by downloading DB from Craft staging site and importing, instead of using Sequel Pro. Also make sure your configs in general.php are correct.


1

I don't have much experience with these, but here are some potential plugins you can look into for Craft 2 that use various anti-spam techniques: https://straightupcraft.com/craft-plugins/snaptcha https://straightupcraft.com/craft-plugins/sprout-invisible-captcha https://straightupcraft.com/craft-plugins/google-recaptcha https://straightupcraft.com/craft-...


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