Hot answers tagged

7

Something I wish I knew a long time ago, as it has bitten us quite a few times already in the past. Nothing as bad as having an automated deployment process that fails once in a while. I'm sharing it here, but I believe it would be good to add it to the knowledge base too (https://craftcms.com/knowledge-base/deployment-best-practices) The solution is to ...


6

Very similar to Devin's answer. I came up against this on Friday and I had to follow these steps to resolve this issue. Nuke the vendor folder within the project. rm -rf vendor/ Nuke the composer.lock file within the project. rm composer.lock Clear the composer cache composer clearcache within the project. Rollback to composer v1 composer self-update --1 ...


6

Unfortunately, the answer differs pretty dramatically for native properties with "stable" column names and for Content, which (as of Craft 3.7.0) now contain a "suffix" that (in most any other case) is not a concern for developers. More on that in a moment. Titles For titles, it's as simple as using the .select() method native to the ...


5

I managed to solve this by running: rm -rf vendor/ rm composer.lock At this point, the error still persisted when running composer update or composer dumpauto but then after running: composer global clearcache everything worked as per normal. My guess is something got corrupted within in composers' global cache. I am not sure if the first two steps are ...


5

Craft uses a fairly typical relational model, where most1 custom fields have a single column in the content table, set to an appropriate column type chosen by the field type. Unlike EAV, which crams all data into a fixed schema, Craft’s schema will change over time based on the custom fields you’ve configured. There are both performance and storage benefits ...


4

The problem seems to be with parsing vendor/composer/installed.json Once I deleted it - install went well. Even without deleting other files. This one is generated one. You can also simply remove whole vendor/composer directory.


4

This is certainly still possible with Commerce 3 and Craft 3. In a custom plugin or module you can listen for the Transactions::EVENT_AFTER_CREATE_TRANSACTION event and amend the transaction amount at this point in the process. For example, you could make your own payment form that includes a partPaymentAmount field and posts to the normal commerce/payments/...


4

The problem is that you are doing .relatedTo(category) but the variable you are passing is called relatedTo, not category. Just like with limit, based on your code, it should be .relatedTo(relatedTo). Also, it should be part of the same hash as your limit. However, you can't pass a category object to a component and will need to pass its id instead (docs). ...


4

When searching the Craft 3.5.x docs for siteName, I see references to Global Variables and General Config Settings. Screenshot: https://cln.sh/egvIXhxZSzaFjeCgNLxX However, AFAICT, it works differently for modules/plugins. As per the Sites section in the Craft 3.5.x Class Reference docs at https://docs.craftcms.com/api/v3/craft-services-sites.html, you would ...


4

So the work around was to convert the Neo field to a Matrix. That worked for this instance since we're not storing anything complex but I'd sure like to figure out if/how Neo supports mutations. mutation addRecipeReview($recipeId: ID, $blocks: [reviews_MatrixBlockContainerInput] = {review: {authorEmail: "", authorName: "", authorReview: &...


4

When looping over the letters in the range, you should be able to use the keys filter to check if the current letter is in the entriesByGroup array. If it is, you can link it or add a class, etc. {% set letterArray = 'A'..'Z' %} {% set entries = craft.entries.section('myEntries').orderBy('title asc').all() %} {% set entriesByGroup = entries|group('title[:1]')...


4

Just to expand on the caveat regarding field suffixes in Craft 3.7+, I asked on Github what the recommended way to include field suffixes in a custom query is. Here's brandonkelly's response: The easiest way to do it would be to just hardcode the column name in your template. It won’t change between environments or anything. That said, it does feel kind of ...


3

Try just POSTing the new absolute quantity you want to change to - e.g. if you have 5 in your cart and want to decrement by one, you'd POST something along these lines: { "redirect": "6bb2a73aac3479c68e0946e4455624e2c1b840bc2b6356074a51ba01ee1234d/cart", "action": "commerce/cart/update-cart", "...


3

I agree that any solution where you run a query and then conditionally render the result isn't very efficient. You can do this in SQL by using the where() condition. craft.entries .section('mySection') .where('content.field_field1 > content.field_field2') .all() Documentation on the where() condition can be found here.


3

Cache poisoning would only happen if you don't hard-set your @web alias manually and it is auto-determined. Set it in your config/general.php with: 'aliases' => [ 'web' => getenv('SITE_BASE_URL'), ], Then you'll be able to revert to using it in your Control Panel. In terms of why this happened; you can't add to an environment variable the way you ...


3

You need to query using strings and your condition should be written as follow: {% set catYear = craft.app.request.segment(-1) %} {% set firstDayOfYear = catYear ~ "-01-01" %} {% set lastDayOfYear = catYear ~ "-12-31" %} {% set params = { section : 'termine', datumTermin : ['and', '>=' ~ firstDayOfYear, '<= ' ~ ...


3

You're passing false as the second parameter of your saveElement call... that parameter means you're skipping the built-in validation which should catch this error for you. If you get rid of that you should then receive a warning in the logs when processing existing users, but it shouldn't halt execution of the script.


3

Twig offers special loop variables that makes it easy to know indexing of a loop. You can use loop.index to count your current indexing of the loop, which will starting at 1. If You want to start your indexing from 0 so simply add 0 after loop.index like loop.index0. There are other loop variable as well. Like: loop.revindex loop.revindex0 loop.first loop....


3

If you don't have tons and tons of creators, you could do that by querying creators and re-arranging the results: {# Get all creators and eager-load the products #} {% set creators = craft.entries() .section('creators') .with('products') .all() %} {# Declare an array to store all related product ID #} {% set products = [] %} {# Build an array ...


3

Yes you can, using the Model::EVENT_DEFINE_RULES Put this in your customer module or plugin. use craft\commerce\elements\Variant; use craft\base\Model; use craft\events\DefineRulesEvent; Event::on( Variant::class, Model::EVENT_DEFINE_RULES, function(DefineRulesEvent $event) { ...


3

You could write a service method that reads the import file and divides it into chunks, then it could dispatch a queue job for each chunk which does the actual entry creation/update. This way you could scale the resources and amount of queue workers available to work through the queue and even Play with the chunk size. In the queue job you might want to ...


3

Why use includeCssFile at all for your core CSS? Typically you'd use that for injecting template-specific CSS files (e.g. carousel scripts) that you don't want to load on every page. (The advantage is that you can call that includeCssFile function in deeply nested templates without worrying about template inheritance or whether another partial has already ...


3

The error indicates that you're type-hinting the wrong class. Note this line in the error message: Argument 1 passed to amss\amss\AMSS::amss\amss{closure}() must be an instance of amss\amss\RegisterComponentTypesEvent, instance of craft\events\RegisterComponentTypesEvent given This comes from the type-hint in your function: function(...


3

Not sure if there is a plugin that does this automatically, but you can build a small module that accomplishes this. Since it should run automatically in the background, I would create the functionality as a custom console command and then execute that command regularly in a cron job (for example, daily or hourly). The command would do the following: Check ...


3

At the moment, you are loading all categories in the line {% set categories = categoryEntry.all() %} and that's why you are getting them all for each entries. What you need to do is to fetch your categories in the context of your blogEntry. Here I assume a field name of blogCategories on your blogEntry but change accordingly to match what your Categories ...


2

I just use the included IIS on Windows 10 for development. Install MySQL and PHP and all runs fairly smoothly. Just recently recently configured this on a new Win10 machine and made a note of another couple of points: Make sure that IIS_IUSRs has full permission to: c:\Windows\Temp and the directory where you will hold your development websites. After up ...


2

Lindsey D's answer above worked for me, the stumper for me was that the Rebuild Search Indexes feature that used to be in the Settings menu in Craft 2 is no longer there in Craft 3, (there is an Asset Indexes page in the Utilities menu, but that's not what we need here). So to rebuild the search index in Craft 3, I needed to do this: In a command window, ...


2

The search parameter relies on the internal search index (whereas your other approach does not). It's possible that something got tripped up during a migration to Craft 3, perhaps when the new drafts & revisions system was introduced. Try rebuilding your search index. It might take a long time to run, but should hopefully solve your problem.


2

The search parameter only searches the element that is the subject of your element query (i.e., the source entry) - it does not search related entries. For that you need to use the relatedTo parameter. (I'm not sure why you're getting a successful result for the November entry - perhaps the source entry also has the word November in it somewhere?). ...


2

Since 3.4, you do this by passing an array of site ID/site-enabled mappings like so: $entry->enabledForSite = [1 => true, 2 => true]; Where 1 and 2 are your chosen site ids.


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