Hot answers tagged

26 votes
Accepted

How to display all fields belonging to current entry?

Field groups are not specific to a particular channel or channel entry. They are just an arbitrary organization within the control panel for ease of use. What you would be looking for are the fields ...
user avatar
16 votes
Accepted

How do we create a pre-defined Asset upload field in a plugin that integrates with the native Craft Assets file manager?

I did this recently with my Ad Wizard plugin... There are several different parts which need to be brought together to make it work: In your Record file, add this to defineRelations: 'asset' => ...
user avatar
  • 23.6k
13 votes
Accepted

How do I maintain the on value with a lightswitch-field as a plugin setting?

I believe you need to set the on value of your lightswitch field. on: (settings.mailerShowInfo is defined) ? settings.mailerShowInfo : "" So, the full settings of your twig code would be: {{ forms....
user avatar
  • 13.2k
11 votes

How to add more options to the 'Restrict allowed file types' settings?

You can now set allowedFileExtensions and extraAllowedFileExtensions in your config file. The first will re-define the list of allowed files, where the latter will allow you to add additional file ...
user avatar
11 votes
Accepted

How do I output a dropdown's values if I'm on a registration form not tied to an entry?

The not so elegant way to do this is to re-use the field somewhere else, where you do have access to (e.g. a global set). But you probably want to avoid this, right? What you can do instead is to ...
user avatar
  • 35.8k
10 votes
Accepted

Get dropdown / checkbox / multiselect field values on frontend submit

You can get info about your fields using craft.fields.getFieldByHandle(). That will include a settings property, which will be an array of the field type’s settings. The actual settings available ...
user avatar
9 votes
Accepted

"Conditional" field layout - hiding or showing fields based on another field

The feature request for this has been very popular since day one, so hopefully it's something P&T adds to the core some day. For the interim I just published a small plugin called Reasons, which ...
user avatar
8 votes
Accepted

How can I test if a field has been assigned to a channel?

Twig has a "is defined" test that you can apply to a variable to see if it exists: {% if entry.possibleField is defined %} Yup! {% else %} Nope! {% endif %} And here is more information on ...
user avatar
8 votes

Can I add an enabled-by-default lightswitch field to existing entries?

If there was no default set beforehand, then your previous elements will have a value of null for that field. You should be able to include those in your "on" queries by querying for that null value ...
user avatar
  • 4,361
7 votes

Exporting/Importing Fields

I always write migrations to do this. Here's and example for changing the label of a field. You could, of course, do whatever you want - modify fieldlayouts, add/modify/remove fields, etc. UPDATE ...
user avatar
7 votes

Will having large numbers of Fields decrease site performance?

I can't answer your question directly as I never got even close to 300 fields yet. But even with 50 fields I had some serious slowdowns, especially when getting related entries in form of assets and ...
user avatar
  • 35.8k
7 votes
Accepted

How can I limit the rows returned in a table field?

According to the Craft docs , the table field returns an array of rows. Assuming that is the case, you should be able to use the Twig slice filter: {% for row in registration.registrationCost | ...
user avatar
7 votes
Accepted

How to use a global variable in a conditional statement if an entries relation field is empty?

The reason it wasn't rendering is because my original entry field is an Entries Field (see templating examples in the docs). The relationship fieldtypes (Entries, Assets, Users) require that you ...
user avatar
  • 605
7 votes

How to clear the database of unused fields?

You can get the fields that aren’t in use anywhere by running this SQL query: SELECT f.* FROM craft_fields f LEFT JOIN craft_fieldlayoutfields l ON l.fieldId = f.id WHERE l.id IS NULL The easiest ...
user avatar
7 votes
Accepted

How can I programmatically add a tab to entry type?

I eventually figured this out. Here's the code that's working for me: $newFields = array( 0 => '12', // $field->fieldId 1 => '15' // $field->fieldId ); // First we get all sections ...
user avatar
7 votes
Accepted

Custom HTML in Rich Text Fields

If your Redactor config included an "html" value in the "buttons" array, you can remove that, and add a "source" value to the "plugins" array instead. https://craftcms.com/help/redactor-ii-configs
user avatar
  • 35.8k
6 votes

Is there a way to populate a drop-down with the templates files in a folder?

This would be a good task for a plugin. You'd essentially need to build the plugin to do a couple of things: (1) Use PHP in a service to list the contents of a directory. Once you have that list, ...
user avatar
  • 23.6k
6 votes

Exporting/Importing Fields

Check out https://github.com/engram-design/FieldManager I was looking for a way to duplicate a matrix field to make another variant. This works perfectly for cloning fields and seems to have Export/...
user avatar
  • 113
6 votes
Accepted

Is there any way of specifying a default category for a category field?

You could output the categories in a way that makes them look like they are from a specific category when they don't have any categories assigned to them. When you're outputting any of the entries, ...
user avatar
  • 2,975
6 votes
Accepted

How do I find the closest parent entry with an Assets field filled in?

You can get your entry's parent entry with getParent(). Use this entry method in a conditional, if your entry has no hero image set. You can nest the code to climb up all of your structure's levels: ...
user avatar
  • 35.8k
6 votes

How do I find the closest parent entry with an Assets field filled in?

Another way to do this would be to assemble a list of the entry IDs that you want to look for a hero image in, in order, and then finding the first hero image related to one of those entries. To ...
user avatar
6 votes

Programatically Adding Fields to a Layout

While I am adding them from a plugin, I ran into a similar issue as you, and like you, I didn't like the "recreate the layout from scratch" method the linked answer used. I know this question is old, ...
user avatar
  • 181
6 votes
Accepted

Clarification on UID creation

As aberkie pointed out, Craft does use them internally, but they should not be left blank or you'll potentially run into some issues now and you'll miss out on some upcoming features. You can ...
user avatar
  • 66.4k
6 votes
Accepted

How is Craft able to reuse fields in different Sections? (As opposed to EE.)

In Craft, all fields are stored in the craft_content table. They are otherwise independent of any field groups, layouts, sections, or anything else. PRO: This means that any element (Entry, Category, ...
user avatar
  • 23.6k
6 votes
Accepted

How does Craft automatically wrap `fields[]` around name attributes?

I believe its the namespace twig template function - and fields typically use the default namespace as fields. This is defined under templates/_includes/fields.html Looking at that file (below), you'...
user avatar
  • 3,392
5 votes

Is there any way of specifying a default category for a category field?

Not with the out-of-the-box field types. You could create a field type that is category+default. The Default Color plugin might be a good place to start.
user avatar
5 votes
Accepted

Is there a way to populate a drop-down with the templates files in a folder?

try using this: https://github.com/lukeholder/dir to populate this plugin with the template names: https://github.com/lewisjenkins/craft-lj-dynamicfields/
user avatar
  • 6,637
5 votes

Entry field not outputting after {% set... %} code in template

Here's your problem... You're redefining entry halfway down the page. This page clearly uses the section's built-in routing functionality. I can tell because you use entry.body near the top of your ...
user avatar
  • 23.6k
5 votes
Accepted

Dynamically iterate over a matrix block's fields?

Not sure what you want to do with the fields, but you can access them through fieldLayout. {% for block in entry.myMatrixField %} {% for field in block.getFieldLayout().getFields() %} {#...
user avatar
5 votes

"Conditional" field layout - hiding or showing fields based on another field

No, Craft doesn't offer conditionals in field layouts. Coming from Wordpress/ACF, I'd personally like to see something like that implemented in the future, but for now I can think of two possible ...
user avatar

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