Skip to content

Commit

Permalink
Merge branch 'release/5.0.0' into v5
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jan 20, 2025
2 parents d2f0c1f + 148476f commit dded4d7
Show file tree
Hide file tree
Showing 23 changed files with 333 additions and 108 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Units Changelog

## 5.0.0 - UNRELEASED
## 5.0.0 - 2025.01.19
### Added
* Initial Craft CMS 5 release
* Add a GraphQL interface for Units fields, closes ([#5](https://github.com/nystudio107/craft-units/issues/5))
43 changes: 43 additions & 0 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,49 @@ The `false` parameter causes it to just list the units, and none of their aliase

In this loop, `aliases` will be an array of aliases for a given unit (if any).

## GraphQL Interface

You can use GraphQL to query Units fields, with an API analogous to the Twig API. In the example below, `someUnits` is the field handle to a Units field:

```graphql
{
entries(section: "homepage") {
... on homepage_Entry {
someUnits {
value
units
toString
toUnit(unit: "m")
toFraction
toUnitFraction(unit: "m")
getValueFraction
}
}
}
}
```
...will return:

```json
{
"data": {
"entries": [
{
"someUnits": {
"value": "12.5",
"units": "ft",
"toString": "12.5 ft",
"toUnit": "3.81",
"toFraction": "12 1/2 ft",
"toUnitFraction": "3 81/100",
"getValueFraction": "12 1/2"
}
}
]
}
}
```

## Units Reference

Units uses the [PHP Units of Measure](https://github.com/PhpUnitsOfMeasure/php-units-of-measure) library, so it offers quite a bit of flexibility.
Expand Down
4 changes: 2 additions & 2 deletions src/Units.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/**
* Units plugin for Craft CMS 3.x
* Units plugin for Craft CMS
*
* A plugin for handling physical quantities and the units of measure in which they're represented.
*
* @link https://nystudio107.com/
* @copyright Copyright (c) 2018 nystudio107
* @copyright Copyright (c) nystudio107
*/

namespace nystudio107\units;
Expand Down
4 changes: 2 additions & 2 deletions src/assetbundles/units/UnitsAsset.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/**
* Units plugin for Craft CMS 3.x
* Units plugin for Craft CMS
*
* A plugin for handling physical quantities and the units of measure in which they're represented.
*
* @link https://nystudio107.com/
* @copyright Copyright (c) 2018 nystudio107
* @copyright Copyright (c) nystudio107
*/

namespace nystudio107\units\assetbundles\units;
Expand Down
2 changes: 1 addition & 1 deletion src/assetbundles/units/dist/css/Units.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Units CSS
*
* @author nystudio107
* @copyright Copyright (c) 2018 nystudio107
* @copyright Copyright (c) nystudio107
* @link https://nystudio107.com/
* @package Units
* @since 1.0.0
Expand Down
58 changes: 29 additions & 29 deletions src/assetbundles/units/dist/js/Units.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Units JS
*
* @author nystudio107
* @copyright Copyright (c) 2018 nystudio107
* @copyright Copyright (c) nystudio107
* @link https://nystudio107.com/
* @package Units
* @since 1.0.0
Expand All @@ -19,33 +19,33 @@
* @param callback
*/
function fillDynamicUnitsMenu(menuId, menuValue, unitsClass, callback) {
var menu = $('#' + menuId);
var menu = $('#' + menuId);

if (menu.length) {
menu.empty();
$.ajax({
url: Craft.getActionUrl('units/units/available-units?unitsClass=' + unitsClass)
})
.done(function(data) {
var newValue = Object.keys(data)[0];
for (var k in data) {
if (data.hasOwnProperty(k)) {
if (k === menuValue) {
newValue = menuValue;
}
$('<option />')
.attr('value', k)
.html(data[k])
.appendTo(menu);
}
}
menu.val(newValue);
if (callback !== undefined) {
callback();
}
})
.fail(function(data) {
console.log('Error loading units data');
})
}
if (menu.length) {
menu.empty();
$.ajax({
url: Craft.getActionUrl('units/units/available-units?unitsClass=' + unitsClass)
})
.done(function (data) {
var newValue = Object.keys(data)[0];
for (var k in data) {
if (data.hasOwnProperty(k)) {
if (k === menuValue) {
newValue = menuValue;
}
$('<option />')
.attr('value', k)
.html(data[k])
.appendTo(menu);
}
}
menu.val(newValue);
if (callback !== undefined) {
callback();
}
})
.fail(function (data) {
console.log('Error loading units data');
})
}
}
4 changes: 2 additions & 2 deletions src/assetbundles/unitsfield/UnitsFieldAsset.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/**
* Units plugin for Craft CMS 3.x
* Units plugin for Craft CMS
*
* A plugin for handling physical quantities and the units of measure in which they're represented.
*
* @link https://nystudio107.com/
* @copyright Copyright (c) 2018 nystudio107
* @copyright Copyright (c) nystudio107
*/

namespace nystudio107\units\assetbundles\unitsfield;
Expand Down
2 changes: 1 addition & 1 deletion src/assetbundles/unitsfield/dist/css/Units.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Units Field CSS
*
* @author nystudio107
* @copyright Copyright (c) 2018 nystudio107
* @copyright Copyright (c) nystudio107
* @link https://nystudio107.com/
* @package Units
* @since 1.0.0
Expand Down
67 changes: 33 additions & 34 deletions src/assetbundles/unitsfield/dist/js/Units.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,51 @@
* Units Field JS
*
* @author nystudio107
* @copyright Copyright (c) 2018 nystudio107
* @copyright Copyright (c) nystudio107
* @link https://nystudio107.com/
* @package Units
* @since 1.0.0UnitsUnits
*/

;(function ( $, window, document, undefined ) {
;(function ($, window, document, undefined) {

var pluginName = "UnitsUnits",
defaults = {
};
var pluginName = "UnitsUnits",
defaults = {};

// Plugin constructor
function Plugin( element, options ) {
this.element = element;
// Plugin constructor
function Plugin(element, options) {
this.element = element;

this.options = $.extend( {}, defaults, options) ;
this.options = $.extend({}, defaults, options);

this._defaults = defaults;
this._name = pluginName;
this._defaults = defaults;
this._name = pluginName;

this.init();
}

Plugin.prototype = {
this.init();
}

init: function(id) {
var _this = this;
Plugin.prototype = {

$(function () {
init: function (id) {
var _this = this;

/* -- _this.options gives us access to the $jsonVars that our FieldType passed down to us */
$(function () {

});
}
};
/* -- _this.options gives us access to the $jsonVars that our FieldType passed down to us */

// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName,
new Plugin( this, options ));
}
});
};

})( jQuery, window, document );
});
}
};

// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName,
new Plugin(this, options));
}
});
};

})(jQuery, window, document);
4 changes: 2 additions & 2 deletions src/config.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/**
* Units plugin for Craft CMS 3.x
* Units plugin for Craft CMS
*
* A plugin for handling physical quantities and the units of measure in which they're represented.
*
* @link https://nystudio107.com/
* @copyright Copyright (c) 2018 nystudio107
* @copyright Copyright (c) nystudio107
*/

use PhpUnitsOfMeasure\PhysicalQuantity\Length;
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/UnitsController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/**
* Units plugin for Craft CMS 3.x
* Units plugin for Craft CMS
*
* A plugin for handling physical quantities and the units of measure in which they're represented.
*
* @link https://nystudio107.com/
* @copyright Copyright (c) 2018 nystudio107
* @copyright Copyright (c) nystudio107
*/

namespace nystudio107\units\controllers;
Expand Down
31 changes: 26 additions & 5 deletions src/fields/Units.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
/**
* Units plugin for Craft CMS 3.x
* Units plugin for Craft CMS
*
* A plugin for handling physical quantities and the units of measure in which
* they're represented.
*
* @link https://nystudio107.com/
* @copyright Copyright (c) 2018 nystudio107
* @copyright Copyright (c) nystudio107
*/

namespace nystudio107\units\fields;
Expand All @@ -18,17 +18,16 @@
use craft\helpers\Html;
use craft\helpers\Json;
use craft\i18n\Locale;
use GraphQL\Type\Definition\Type;
use nystudio107\units\assetbundles\unitsfield\UnitsFieldAsset;
use nystudio107\units\gql\types\generators\UnitsDataGenerator;
use nystudio107\units\helpers\ClassHelper;
use nystudio107\units\models\Settings;
use nystudio107\units\models\UnitsData;
use nystudio107\units\Units as UnitsPlugin;
use nystudio107\units\validators\EmbeddedUnitsDataValidator;
use PhpUnitsOfMeasure\PhysicalQuantity\Length;
use yii\base\InvalidConfigException;
use function is_array;
use function is_numeric;
use function is_string;

/**
* @author nystudio107
Expand Down Expand Up @@ -84,6 +83,14 @@ public static function displayName(): string
return Craft::t('units', 'Units');
}

/**
* @inheritdoc
*/
public static function icon(): string
{
return 'scale-balanced';
}

// Public Methods
// =========================================================================

Expand Down Expand Up @@ -246,6 +253,20 @@ public function getInputHtml(mixed $value, ?ElementInterface $element = null): s
return '';
}

/**
* @inheritdoc
*/
public function getContentGqlType(): Type|array
{
$typeArray = UnitsDataGenerator::generateTypes($this);

return [
'name' => $this->handle,
'description' => 'Units field',
'type' => array_shift($typeArray),
];
}

/**
* @inheritdoc
*/
Expand Down
Loading

0 comments on commit dded4d7

Please sign in to comment.