Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

Commit

Permalink
Adds support for Solspace Calendar, and relational fieldtypes as togg…
Browse files Browse the repository at this point in the history
…le fields. Bump to 1.0.5
  • Loading branch information
mmikkel committed Jul 6, 2016
1 parent 122838b commit b3cd28e
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 58 deletions.
47 changes: 35 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Reasons v. 1.0.4 for Craft CMS ![Craft 2.5](https://img.shields.io/badge/craft-2.5-red.svg?style=flat-square)
# Reasons v. 1.0.5 for Craft CMS ![Craft 2.5](https://img.shields.io/badge/craft-2.5-red.svg?style=flat-square)

_Supercharge your field layouts with conditionals._

Expand All @@ -9,10 +9,8 @@ Inspired by Advanced Custom Fields for WordPress, Reasons adds simple conditiona
_Big props to [Benjamin Fleming](https://github.com/benjamminf) for some good ideas that I stole, and helpful chatting on Slack. Thanks!_

## Recent updates
* Support for all native element types (Entries, Assets, Categories, Tags, Users and Global Sets)
* Support for [Tag Manager](https://github.com/boboldehampsink/tagmanager)
* Support for all native element editor modals
* Support for Live Preview
* Support for all native relational fields (Entries, Assets, Categories, Tags and Users) as toggle fields
* Support for Solspace Calendar

## Requirements

Expand All @@ -24,8 +22,6 @@ _Big props to [Benjamin Fleming](https://github.com/benjamminf) for some good id
* Move the `/reasons` folder to your `/craft/plugins` folder
* Install from the Control Panel (`/admin/settings/plugins`)

**Important: Reasons requires Craft 2.5+.** Need Craft 2.4 or 2.3 support? Check out the [legacy branch](https://github.com/mmikkel/Reasons-Craft/tree/legacy).

## Usage and common questions

![Setting up conditionals using the built-in field layout designer](http://g.recordit.co/R7Ti1xpL9Q.gif)
Expand All @@ -36,9 +32,9 @@ Note that for element sources that support tabbed field layouts, Reasons is desi

### What's a toggle field?

A _toggle field_ in Reasons is a field that can be used to "toggle" another field (the _target field_) on or off (show or hide, as it were). This works by comparing the value of the toggle field declared inside the Field Layout Designer, to the current value of that same field when the content is edited.
A _toggle field_ in Reasons is a field that can be used to "toggle" another field (the _target field_) on or off (show or hide, as it were).

The following FieldTypes can be used as toggle fields:
The following stock FieldTypes can be used as toggle fields:

* Lightswitch
* Dropdown
Expand All @@ -48,12 +44,31 @@ The following FieldTypes can be used as toggle fields:
* Number
* Position Select
* Plain Text
* Entries
* Categories
* Tags
* Assets
* Users

The following custom FieldTypes are also supported:

* Solspace Events

### Where does it work?

All FieldTypes (even custom ones) can be target fields.
Reasons currently works for

### Does Reasons work inside Matrix fields?
* Entries
* Categories
* Tags
* Assets
* Users
* Tag Manager
* Solspace Calendar Events

Nope, not yet. Work is underway for Matrix block type support, but it's pretty difficult and there's no ETA, unfortunately.
Reasons also works in Live Preview and Element Editor modals.

Unfortunately, Reasons does _not_ work inside Matrix blocks. Matrix support is hopefully coming in the future.

### A note on required fields

Expand Down Expand Up @@ -94,6 +109,14 @@ Please report any bugs, feature requests or other issues [here](https://github.c

### Changelog

#### 1.0.5 (07.06.2016)

* [Added] Support for Entries, Assets, Tags, Users and Categories fieldtypes as toggle fields
* [Added] Support for Solspace Calendar Events fieldtype as toggle fields
* [Added] Full support for Solspace Calendar
* [Improved] Reasons will no longer cache conditionals if devMode is enabled
* [Improved] Hidden fields are no longer tabbable/focusable

#### 1.0.4 (05.25.2016)

* [Fixed] Fixes a rare issue where Reasons would hide fields inside Matrix blocks
Expand Down
64 changes: 43 additions & 21 deletions reasons/ReasonsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
class ReasonsPlugin extends BasePlugin
{

protected $_version = '1.0.4',
$_schemaVersion = '1.1',
$_developer = 'Mats Mikkel Rummelhoff',
$_developerUrl = 'http://mmikkel.no',
$_pluginName = 'Reasons',
$_pluginUrl = 'https://github.com/mmikkel/Reasons-Craft',
$_releaseFeedUrl = 'https://raw.githubusercontent.com/mmikkel/Reasons-Craft/master/releases.json',
$_documentationUrl = 'https://github.com/mmikkel/Reasons-Craft/blob/master/README.md',
$_description = 'Adds conditionals to field layouts.',
$_minVersion = '2.5';
protected $_version = '1.0.5';
protected $_schemaVersion = '1.1';
protected $_developer = 'Mats Mikkel Rummelhoff';
protected $_developerUrl = 'http://mmikkel.no';
protected $_pluginName = 'Reasons';
protected $_pluginUrl = 'https://github.com/mmikkel/Reasons-Craft';
protected $_releaseFeedUrl = 'https://raw.githubusercontent.com/mmikkel/Reasons-Craft/master/releases.json';
protected $_documentationUrl = 'https://github.com/mmikkel/Reasons-Craft/blob/master/README.md';
protected $_description = 'Adds conditionals to field layouts.';
protected $_minVersion = '2.5';

/**
* @return string
Expand Down Expand Up @@ -162,7 +162,7 @@ public function init()
$this->includeResources();

craft()->templates->includeJs('if (window.Craft && window.Craft.ReasonsPlugin) {
Craft.ReasonsPlugin.init(' . $this->getData() . ');
Craft.ReasonsPlugin.init('.$this->getData().');
}');

craft()->on('fields.saveFieldLayout', array($this, 'onSaveFieldLayout'));
Expand Down Expand Up @@ -268,14 +268,19 @@ protected function includeResources()
*/
protected function getData()
{
if (!$data = craft()->fileCache->get($this->getCacheKey())) {
$doCacheData = !craft()->config->get('devMode');
$cacheKey = $this->getCacheKey();
$data = $doCacheData ? craft()->fileCache->get($cacheKey) : null;
if (!$data) {
$data = array(
'conditionals' => $this->getConditionals(),
'toggleFieldTypes' => $this->getToggleFieldTypes(),
'toggleFields' => $this->getToggleFields(),
'fieldIds' => $this->getFieldIds(),
);
craft()->fileCache->set($this->getCacheKey(), $data, 1800); // Cache for 30 minutes
if ($doCacheData) {
craft()->fileCache->set($this->getCacheKey(), $data, 1800); // Cache for 30 minutes
}
}
return json_encode($data);
}
Expand Down Expand Up @@ -323,21 +328,30 @@ protected function getConditionals()
$sources['globalSet:' . $globalSet->id] = $globalSet->fieldLayoutId;
}

// Matrix blocks
$matrixBlockTypeRecords = MatrixBlockTypeRecord::model()->findAll();
if ($matrixBlockTypeRecords) {
foreach ($matrixBlockTypeRecords as $matrixBlockTypeRecord) {
$matrixBlockType = MatrixBlockTypeModel::populateModel($matrixBlockTypeRecord);
$sources['matrixBlockType:' . $matrixBlockType->id] = $matrixBlockType->fieldLayoutId;
}
}
// Matrix blocks – TODO
// $matrixBlockTypeRecords = MatrixBlockTypeRecord::model()->findAll();
// if ($matrixBlockTypeRecords) {
// foreach ($matrixBlockTypeRecords as $matrixBlockTypeRecord) {
// $matrixBlockType = MatrixBlockTypeModel::populateModel($matrixBlockTypeRecord);
// $sources['matrixBlockType:' . $matrixBlockType->id] = $matrixBlockType->fieldLayoutId;
// }
// }

// Users
$usersFieldLayout = craft()->fields->getLayoutByType(ElementType::User);
if ($usersFieldLayout) {
$sources['users'] = $usersFieldLayout->id;
}

// Solspace Calendar
$solspaceCalendarPlugin = craft()->plugins->getPlugin('calendar');
if ($solspaceCalendarPlugin && $solspaceCalendarPlugin->getDeveloper() === 'Solspace') {
$solspaceCalendarFieldLayout = craft()->fields->getLayoutByType('Calendar_Event');
if ($solspaceCalendarFieldLayout) {
$sources['solspaceCalendar'] = $solspaceCalendarFieldLayout->id;
}
}

// Get all conditionals
$conditionals = array();
$conditionalsRecords = Reasons_ConditionalsRecord::model()->findAll();
Expand Down Expand Up @@ -367,6 +381,7 @@ protected function getConditionals()
protected function getToggleFieldTypes()
{
return array(
// Stock FieldTypes
'Lightswitch',
'Dropdown',
'Checkboxes',
Expand All @@ -375,6 +390,13 @@ protected function getToggleFieldTypes()
'Number',
'PositionSelect',
'PlainText',
'Entries',
'Categories',
'Tags',
'Assets',
'Users',
// Custom FieldTypes
'Calendar_Event',
);
}

Expand Down

This file was deleted.

8 changes: 8 additions & 0 deletions reasons/resources/javascripts/reasons-6738d5f6a670e6e80807.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions reasons/resources/rev-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"javascripts/reasons.js": "javascripts/reasons-6124c2e40a6cb71616a5.js",
"stylesheets/reasons.css": "stylesheets/reasons-a008e38a64.css"
"javascripts/reasons.js": "javascripts/reasons-6738d5f6a670e6e80807.js",
"stylesheets/reasons.css": "stylesheets/reasons-2b40a218fb.css"
}
12 changes: 12 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
[
{
"version": "1.0.5",
"downloadUrl": "https://github.com/mmikkel/Reasons-Craft/archive/master.zip",
"date": "2016-07-06T10:00:00-08:00",
"notes": [
"[Added] Support for Entries, Assets, Tags, Users and Categories fieldtypes as toggle fields",
"[Added] Support for Solspace Calendar Events fieldtype as toggle fields",
"[Added] Full support for Solspace Calendar",
"[Improved] Reasons will no longer cache conditionals if devMode is enabled",
"[Improved] Hidden fields are no longer tabbable/focusable"
]
},
{
"version": "1.0.4",
"downloadUrl": "https://github.com/mmikkel/Reasons-Craft/archive/master.zip",
Expand Down
27 changes: 20 additions & 7 deletions source/javascripts/modules/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ var ConditionalsBuilder = Garnish.Base.extend({
switch (toggleFieldType) {

// Lightswitch - true/false
case 'Lightswitch' :
case 'Lightswitch':
ruleValueContent = this.templates.select([
{ true : Craft.t('on') },
{ false : Craft.t('off') }
]);
break;

// Option based inputs
case 'Dropdown' : case 'MultiSelect' : case 'Checkboxes' : case 'RadioButtons' :
case 'Dropdown': case 'MultiSelect': case 'Checkboxes': case 'RadioButtons':
var values = toggleFieldSettings.options,
options = [],
option;
Expand All @@ -324,20 +324,20 @@ var ConditionalsBuilder = Garnish.Base.extend({
break;

// Number input
case 'Number' :
case 'Number':
ruleValueContent = this.templates.number(toggleFieldSettings);
break;

// // Color input
// case 'Color' :
// case 'Color':
// toggleFieldSettings = {
// placeholder : '#'
// };
// ruleValueContent = this.templates.input(toggleFieldSettings);
// break;

// Position Select
case 'PositionSelect' :
case 'PositionSelect':
var values = toggleFieldSettings.options,
options = [],
option;
Expand All @@ -349,6 +349,19 @@ var ConditionalsBuilder = Garnish.Base.extend({
ruleValueContent = this.templates.select(options);
break;

// Relational fields
case 'Entries': case 'Categories': case 'Tags': case 'Assets': case 'Users': case 'Calendar_Event':
var options = [
{
'null': Craft.t('Empty').toLowerCase()
},
{
'notnull': Craft.t('Not empty').toLowerCase()
}
];
ruleValueContent = this.templates.select(options);
break;

// Just render a plain text input for anything else
default :
ruleValueContent = this.templates.input(toggleFieldSettings);
Expand Down Expand Up @@ -387,11 +400,11 @@ var ConditionalsBuilder = Garnish.Base.extend({
label = option[value];
selectOptions.push('<option value="' + value + '">' + label + '</option>');
}
return '<div class="' + (isMultiSelect ? 'multiselect' : 'select') + '"><select' + (isMultiSelect ? ' multiple' : '') + '>' + selectOptions.join('') + '</select></div>';
return '<div class="' + (isMultiSelect ? 'multiselect': 'select') + '"><select' + (isMultiSelect ? ' multiple': '') + '>' + selectOptions.join('') + '</select></div>';
},
toggleSelectOption : function(toggleField, selected)
{
return '<option value="' + toggleField.id + '" data-type="' + toggleField.type + '"' + (selected ? ' selected' : '') + '>' + toggleField.name + '</option>';
return '<option value="' + toggleField.id + '" data-type="' + toggleField.type + '"' + (selected ? ' selected': '') + '>' + toggleField.name + '</option>';
},
number : function(settings)
{
Expand Down
Loading

0 comments on commit b3cd28e

Please sign in to comment.