Hot answers tagged

3

I have found that the best format for me is XML, it's easy to write and read. I've moved a number of EE2 sites to Craft by writing export templates using XML and importing into Craft with Feedme. It's relatively simple, but can be tedious and time consuming as there are often tweaks needed either in the XML or the feedme settings. I have a blog article I ...


3

You're close! Because an asset field can potentially contain multiple items you need to explicitly select the first one, so: {% set video = component.video.one() %} But that doesn't get the actual url of the video, to get that you need to request it: {{ video.url() }} So in your case: {% set video = component.video.one() %} {% if video %} <video width=&...


3

Try using {site.handle} instead of {{currentSite.handle}} and it should work ok.


3

This is a general structure to display global images in any template: <img src="{{ globalHandle.globalAssetHandle.one().getUrl }}" alt=""> You have to set your global handle here: And your global asset handle here:


3

Currently, there’s no direct way of doing this. You can pass an existing folder ID but you can’t create folders on the fly. However, you can create folders via an asset field (with dynamic path) attached to an entry type so there is an option that could work if you really need this done. Create a section/entry type that has two fields, an Asset field with a ...


3

I'm guessing you may have found an answer to this, but have you seen the download assets plugin? It puts a download button next to every asset on an edit page that links to the asset's url.


3

Asset fields return like an array so they can potentially have more than one item, you need to request the first item only with .one() (as opposed to requesting .all()) //First check if the asset field has more than zero items {% if block.video.videoFileWebm|length %} // Retrieve the first and only item in the asset field <source src="{{ ...


2

You could try something like '@assetBaseUrl' => $_SERVER['HTTPS'] ? 'https://site1.domain.org/files' : 'http://site1.domain.org/files' which checks for a secure request.


2

Interesting problem! I took a stab at this, see if this works for your use case. It relies on ImageMagick to check for the top left pixel color (0,0) after the image is uploaded (but will also work if you hit resave). (If you want to check the other 3 corners, you'll need more code but this should get you started.) To use it, you'll need a lightswitch field ...


2

In Craft 3 this should be: {% if entry.images | length > 1 %} {% set secondImage = entry.images.1 %} {% endif %}


2

The Volumes service is available via craft.app.volumes, so you can do {% set volume = craft.app.volumes.getVolumeByHandle('volumeHandle') %} {{ volume.name }} or {% set volume = craft.app.volumes.getVolumeById(123) %} {{ volume.name }} Additionally, if you have an Asset model available, you can get its volume like this: {% set volume = asset.volume %} {{ ...


2

Assuming you are on a front end entry form editing an entry, there are a few steps to this. First get all assets a user can choose from: {% set allImages = myAssetQuery.all() %} Next, get the ids of already chosen assets: {% set alreadyChosenImages = entry.businessStandardImages.ids() %} Loop over all images, check the ones already selected: {% for image ...


2

Using a custom module it's easy to simply hide the Assets section of the CP. Users would still be able to access the assets library by entering manually the URL but this is good enough for my use case. Here's the (simplified) code: <?php namespace modules\appModule; use Craft; use craft\events\RegisterCpNavItemsEvent; use craft\web\twig\variables\Cp; ...


2

In the returned object I'm expecting to see an array somewhere for custom field values listing the asset Id's, but it's drawing a blank The reason for this is that entry fields are lazy-loaded, meaning Craft only queries the values from the database once you access them using their field handle (or execute the entity query you get through the field handle, ...


2

The part you're referring to in the docs is when you're importing assets into a volume, not when importing assets as part of entries. I'm not sure how you have things set up, but you could create a new assets volume for your instagram posts and import to that instead. You can then use the title, or any other bit of data to rename the file (I believe). You ...


2

The issue is because wkhtmltopdf wasn't installed on the right server. I need to install the package inside the nitro server by ssh into the root of the container. Here is the steps, nitro ssh --root apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/v3.10/main/ wkhtmltopdf=0.12. 6-r0 wkhtmltopdf=0.12.6-r0 is the version. You might need ...


2

By default, Craft will defer creating image transforms to avoid slowing down page loads. Instead, transforms are queued and executed asynchronously. You can change this behaviour to always generate transforms directly using the generateTransformsBeforePageLoad config setting. Of course, you don't have control over the config if you're working on a plugin. ...


1

ImagickException: data encoding scheme is not supported `/var/www/html/storage/runtime/temp/P0LY_Test60a2e191a91de2.16978634.webp' @ error/webp.c/ReadWEBPImage/338 in /var/www/html/vendor/pixelandtonic/imagine/src/Imagick/Imagine.php:69 Based on that error, it definitely seems like an Imagick bug or Imagick configuration issue. https://github.com/...


1

Caveat: This is totally untested, and could be nonsense. I'm sure you'll let me know. It assumes that the Craft element query methods can be used here. If you have a 'lang' value, add a where to your basic assets element query {% set folderId = craft.app.request.getQueryParam('folderId') %} {% set query = craft.app.request.getQueryParam('q') %} {%...


1

Have you tried the $element->isFieldDirty() method, or checking through the $element->dirtyFields array for your asset field, rather than checking revision dates? There may be a good reason why you're not using those, but I thought I'd suggest it in case you hadn't considered it.


1

There's a couple different ways to handle this. Base URL on Asset Volume One way is just to treat the asset volume like you might a CDN. Unlike other CMS (ahem WP), Craft doesn't hardcode URLs into the database and will build them up on-the-fly. On the local dev server, you just need to set the base url (i.e. server name) of a volume to that of the ...


1

You can use Relations to find assets which are related to a set of entries (i.e. used in a field of the entries). Specifically, the relatedTo parameter allows you to limit an asset query to assets that are used in a specific entry or set of entries. One caveat is that you have to load the entries which contain the assets field first (at least as far as I can ...


1

I figured out this issue was caused by a permissions issue on Amazon AWS. The site was not able to write to the S3 bucket, due to AWS blocking public access to the bucket, which was over-riding the policy I'd applied to the bucket.


1

With the correct cache busting, files can remain cached for as long as you like. Craft handles a lot of this for you. I typically cache items for a year but do take caution to ensure every reference to assets within the site has a cache busting string on it. It's important to note that Cloudfront needs to be setup to use these query strings. On the frontend, ...


1

We have isolated the issue to a crone job connected with the Remote Backup Plugin for Craft. Commenting out the following stops the daily backup. # /home/forge/www.example.com/craft remote-backup/volume/create # /home/forge/www.example.com/craft remote-backup/volume/prune I think there is an issue with trying to connect to the remote storage location.


1

Try using replaceAssetFile(), something like this (untested): $rename = Craft::$app->assets->replaceAssetFile($asset->id,$asset->getFolder(),$newFileName);


1

Have a look at the settings of your asset's volume: Go to: Settings > Assets > [Your Volume Name here] Check the field "Base URL". If it starts with the alias @web, remove it. E.g. "@web/images" -> "/images". Now check the URL of your image again.


1

There's a plugin that you can use to find unused assets: Asset Usage This gives you an extra column in the Assets listing to indicate if an asset is used or not. Unfortunately, the Craft 2 version doesn't have the console command to delete all unused assets that the Craft 3 version does, but this may help you anyway.


1

Does deleting an original asset but keeping the transform folder asset make Craft replace the original file with the biggest transform? No - Craft will always use the originally uploaded asset as the "source of truth" for generating new transforms. You can, however, replace the originally uploaded 8mb+ file with a smaller one, and it'll be used as ...


1

Figured it out: {% set query = craft.assets.volume('volumeHandle').one() %} {{ query.volume.name }}


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