11 votes
Accepted

How does a plugin include it's own config options?

You can add a config.php file in the root of your plugin's folder (craft/plugins/pluginhandle/config.php) that specifies the default values of your config items by returning an array of key value ...
user avatar
  • 66.5k
9 votes
Accepted

Populating tables and matrix fields using GuestEntries

See here for example front-end HTML code for all of Craft's default field types: https://straightupcraft.com/articles/craft-cms-field-guide-twig?view=input Matrix: {# You need to include the 'type' ...
user avatar
  • 66.5k
8 votes

How do I prevent Twig from encoding HTML?

Whenever you output anything with Twig’s {{ output tags }}, the contents will get encoded by default. You have to pass the content through the |raw filter in order for it to be output verbatim. {{ ...
user avatar
6 votes
Accepted

Custom Field Type with Multiple Values

You can do this simply by setting your field type’s defineContentAttribute() method to return AttributeType::Mixed, which will tell Craft to JSON-encode its value before storing it in the database. ...
user avatar
6 votes
Accepted

How to create code blocks with syntax highlighting

There is the Ace Freely plugin, which adds an Ace Editor Field Type that would work well in a Matrix field.
user avatar
  • 4,361
6 votes
Accepted

Are there callbacks for elementSelect macro?

You can get the Element Select’s JS instance like so: $(document).ready(function() { var elementSelect = $('#your-container-id').data('elementSelect'); }); (Replace your-container-id with whatever ...
user avatar
5 votes
Accepted

How to use the native datepicker ui in custom fieldTypes?

Here’s what your field type’s getInputHtml() method should look like: public function getInputHtml($name, $value) { // If $value['trial_end'] is set, make sure that it's a DateTime object if (...
user avatar
5 votes
Accepted

How to reference the current entry element in custom Control Panel templates?

As you discovered, you can get the current element that a field type is getting applied to via $this->element. However you must keep in mind that that property will not always be set. So make sure ...
user avatar
5 votes

Validating a field value is unique

If anybody is interested, I created a simple unique text field plugin that extends the core text field: https://github.com/fuel-digital/Craft-Unique-Text-FieldType.
user avatar
  • 1,503
5 votes

How to create custom field type in Craft 3

In order to include a custom field type you first need to register your own module or plugin you can create the basic structure for both from pluginfactory.io and register them as you like. There is ...
user avatar
4 votes
Accepted

How do I update the content of a field type in the onAfterElementSave() method?

This will return the content for the specific element instance you're on: $this->element->getContent() And from there you can do your additional content processing. When you're ready so save ...
user avatar
  • 66.5k
4 votes
Accepted

Is there a way to generate a url to an image in the craft/storage/myplugin directory?

Since craft/storage/ is not publicly accessible, your only real option here is to serve them through Craft as Resource Requests. (This is the vehicle through which Craft serves up all of the CP’s ...
user avatar
4 votes

How do you customize what value a Custom FieldType returns to a template

Run your HTML string through getRaw before sending it back to the template... return TemplateHelper::getRaw($output); This will basically "pre-raw" your HTML string.
user avatar
  • 23.6k
4 votes
Accepted

Validating a field value is unique

I came up with an answer, but be good to know if there is a better way of doing it: /** * Checks if another entry exists with the given reference * * @param string $value The reference to check ...
user avatar
  • 1,503
4 votes

How to store json data in a custom fieldtype so that it's data can be referenced as attribute values?

You already mentioned the related answer in custom fieldtype with multiple values, and it actually is perfect for your usecase. It doesn't really matter how you transport the data over to Craft, ...
user avatar
  • 2,957
4 votes
Accepted

Custom field type file upload error [Uploaded File Class]

Yii’s CUploadedFile class actually doesn’t support retrieving uploaded files that came from a file input whose name was “namespaced” (e.g. name="fields[handle]" rather than just name="handle"). ...
user avatar
4 votes

How to create code blocks with syntax highlighting

On Straight up Craft we use a Doxter Block for Markdown and Rainbow.js with a custom Craft-Twig extension for syntax highlighting. Doxter allows you to identify the code block like so: ``` twig {{ ...
user avatar
  • 13.3k
4 votes
Accepted

onAfterElementSave and new elements

You should be using it like this: public function onAfterElementSave() { $fieldHandle = $this->model->handle; if ($this->element!==null) { $myData = $this->element->...
user avatar
4 votes
Accepted

Getting Matrix field ID and Block ID for my Custom Field

The BaseFieldType class (which all FieldTypes extend) has a contentPostLocation property, which will give you the "path" to the field inside the entry model. The value is returned as a string, e.g. ...
user avatar
4 votes
Accepted

Why does field value get updated on $element->getContent()->fieldHandle but not when accessed directly $element->fieldHandle?

setContentFromPost() performs an operation on the attached ContentModel, not on the element model itself. When you initialize a BaseElementModel, its content attributes are set from the associated ...
user avatar
  • 3,400
4 votes
Accepted

Adding Columns to a table

With the supercool table maker you can create dynamic tables. Is this what you are looking for?
user avatar
4 votes
Accepted

How to query entries by field values

There is a related to parameter you can use Entry::find() ->section($section) ->relatedTo(['targetElement' => $image->id, 'field' => 'galleryImages') ->all();
user avatar
3 votes
Accepted

Why won't the values of an editableTableField show up in my template?

That string that Craft stores is just JSON. When you try to save an array, Craft will automatically JSON-encode it, since MySQL doesn’t have an array-based data type. To get that string to be ...
user avatar
3 votes

How to store json data in a custom fieldtype so that it's data can be referenced as attribute values?

Completely untested, but a possibility would be to define your content attribute like so: return array(AttributeType::Mixed, 'model' => 'MyPluginContentModel'); MyPluginContentModel would have ...
user avatar
  • 66.5k
3 votes

How do I prevent Twig from encoding HTML?

Try this: public function getFunctions() { return array ( 'my_func' => new \Twig_SimpleFunction('my_func', array($this, 'my_func'), array('is_safe' => array('html'))) ); } This ...
user avatar
  • 396
3 votes

Custom Field Type with Multiple Values

Something like this (Let's call the fieldtype Geo): Your field html should output those two fields, and update a hidden input with the correct namespaced fieldname. Get it like this: $id = craft()-&...
user avatar
  • 2,957
3 votes
Accepted

Plot absolute coordinates / Image map

Have a look at Iain Urquhart's plugin, Pointy: https://github.com/iainurquhart/Craft-Pointy It's a port of his EE addon that's similar to Entry Mapper, but I'm not 100% certain if it's production ...
user avatar
3 votes

How does a plugin output JS that can be used within a cache tag?

That’s a tricky one, because right now the {% cache %} tag is only focussed on the actual HTML that gets output between its tags, but when you include Javascript using TemplatesService::includeJs, etc....
user avatar
3 votes
Accepted

What is the expected data type to be returned when using the prepValue() method?

Looks like the error is occurring when your field type’s getSearchKeywords() function is called. The value that’s passed into that function will have already been run through prepValue(), so at that ...
user avatar
3 votes

Can a field type detect if it's being edited inline in Craft 2.3?

You could check if the whole request is Live Preview using craft.request.isLivePreview.
user avatar

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