├── phpstan.neon ├── src ├── assetbundles │ ├── units │ │ ├── dist │ │ │ ├── css │ │ │ │ └── Units.css │ │ │ ├── js │ │ │ │ └── Units.js │ │ │ └── img │ │ │ │ └── Units-icon.svg │ │ └── UnitsAsset.php │ └── unitsfield │ │ ├── dist │ │ ├── css │ │ │ └── Units.css │ │ ├── js │ │ │ └── Units.js │ │ └── img │ │ │ └── Units-icon.svg │ │ └── UnitsFieldAsset.php ├── gql │ └── types │ │ ├── UnitsDataType.php │ │ └── generators │ │ └── UnitsDataGenerator.php ├── templates │ ├── _components │ │ └── fields │ │ │ ├── Units_input.twig │ │ │ └── Units_settings.twig │ └── settings.twig ├── translations │ └── en │ │ └── units.php ├── config.php ├── helpers │ ├── ClassHelper.php │ └── ClassMapGenerator.php ├── controllers │ └── UnitsController.php ├── models │ ├── Settings.php │ └── UnitsData.php ├── icon.svg ├── validators │ └── EmbeddedUnitsDataValidator.php ├── Units.php ├── variables │ └── UnitsVariable.php └── fields │ └── Units.php ├── ecs.php ├── CHANGELOG.md ├── Makefile ├── LICENSE.md ├── composer.json └── README.md /phpstan.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - %currentWorkingDirectory%/vendor/craftcms/phpstan/phpstan.neon 3 | 4 | parameters: 5 | level: 5 6 | paths: 7 | - src 8 | -------------------------------------------------------------------------------- /src/assetbundles/units/dist/css/Units.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Units plugin for Craft CMS 3 | * 4 | * Units CSS 5 | * 6 | * @author nystudio107 7 | * @copyright Copyright (c) nystudio107 8 | * @link https://nystudio107.com/ 9 | * @package Units 10 | * @since 1.0.0 11 | */ 12 | -------------------------------------------------------------------------------- /ecs.php: -------------------------------------------------------------------------------- 1 | paths([ 8 | __DIR__ . '/src', 9 | __FILE__, 10 | ]); 11 | $ecsConfig->parallel(); 12 | $ecsConfig->sets([SetList::CRAFT_CMS_4]); 13 | }; 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Units Changelog 2 | 3 | ## 5.0.1 - 2025.06.13 4 | ### Fixed 5 | * Pinned the `php-units-of-measure` to version `~2.1.0` to avoid breaking changes in non-major releases 6 | 7 | ## 5.0.0 - 2025.01.19 8 | ### Added 9 | * Initial Craft CMS 5 release 10 | * Add a GraphQL interface for Units fields, closes ([#5](https://github.com/nystudio107/craft-units/issues/5)) 11 | -------------------------------------------------------------------------------- /src/assetbundles/unitsfield/dist/css/Units.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Units plugin for Craft CMS 3 | * 4 | * Units Field CSS 5 | * 6 | * @author nystudio107 7 | * @copyright Copyright (c) nystudio107 8 | * @link https://nystudio107.com/ 9 | * @package Units 10 | * @since 1.0.0 11 | */ 12 | 13 | .units-field-units { 14 | margin: -20px 9px; 15 | } 16 | 17 | .units-field-units-select { 18 | margin: -16px 0px; 19 | } 20 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | MAJOR_VERSION?=5 2 | PLUGINDEV_PROJECT_DIR?=/Users/andrew/webdev/sites/plugindev/cms_v${MAJOR_VERSION}/ 3 | VENDOR?=nystudio107 4 | PROJECT_PATH?=${VENDOR}/$(shell basename $(CURDIR)) 5 | 6 | .PHONY: dev docs release 7 | 8 | # Start up the buildchain dev server 9 | dev: 10 | # Start up the docs dev server 11 | docs: 12 | ${MAKE} -C docs/ dev 13 | # Run code quality tools, tests, and build the buildchain & docs in preparation for a release 14 | release: --code-quality --code-tests --buildchain-clean-build --docs-clean-build 15 | # The internal targets used by the dev & release targets 16 | --buildchain-clean-build: 17 | --code-quality: 18 | ${MAKE} -C ${PLUGINDEV_PROJECT_DIR} -- ecs check vendor/${PROJECT_PATH}/src --fix 19 | ${MAKE} -C ${PLUGINDEV_PROJECT_DIR} -- phpstan analyze -c vendor/${PROJECT_PATH}/phpstan.neon 20 | --code-tests: 21 | --docs-clean-build: 22 | ${MAKE} -C docs/ clean 23 | ${MAKE} -C docs/ image-build 24 | ${MAKE} -C docs/ fix 25 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 nystudio107 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /src/assetbundles/units/UnitsAsset.php: -------------------------------------------------------------------------------- 1 | sourcePath = "@nystudio107/units/assetbundles/units/dist"; 32 | 33 | $this->depends = [ 34 | CpAsset::class, 35 | ]; 36 | 37 | $this->js = [ 38 | 'js/Units.js', 39 | ]; 40 | 41 | $this->css = [ 42 | 'css/Units.css', 43 | ]; 44 | 45 | parent::init(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/assetbundles/unitsfield/UnitsFieldAsset.php: -------------------------------------------------------------------------------- 1 | sourcePath = "@nystudio107/units/assetbundles/unitsfield/dist"; 32 | 33 | $this->depends = [ 34 | CpAsset::class, 35 | ]; 36 | 37 | $this->js = [ 38 | 'js/Units.js', 39 | ]; 40 | 41 | $this->css = [ 42 | 'css/Units.css', 43 | ]; 44 | 45 | parent::init(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/gql/types/UnitsDataType.php: -------------------------------------------------------------------------------- 1 | fieldName; 31 | // Special-case to `toString` since __ prefixes are reserved in GQL 32 | if ($fieldName === 'toString') { 33 | $fieldName = '__toString'; 34 | } 35 | // If the method exists, call it with the passed in args. Otherwise try to retur a property 36 | if (method_exists($source, $fieldName)) { 37 | return $source->$fieldName(...$arguments); 38 | } else { 39 | return $source->$fieldName; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/assetbundles/unitsfield/dist/js/Units.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Units plugin for Craft CMS 3 | * 4 | * Units Field JS 5 | * 6 | * @author nystudio107 7 | * @copyright Copyright (c) nystudio107 8 | * @link https://nystudio107.com/ 9 | * @package Units 10 | * @since 1.0.0UnitsUnits 11 | */ 12 | 13 | ;(function ($, window, document, undefined) { 14 | 15 | var pluginName = "UnitsUnits", 16 | defaults = {}; 17 | 18 | // Plugin constructor 19 | function Plugin(element, options) { 20 | this.element = element; 21 | 22 | this.options = $.extend({}, defaults, options); 23 | 24 | this._defaults = defaults; 25 | this._name = pluginName; 26 | 27 | this.init(); 28 | } 29 | 30 | Plugin.prototype = { 31 | 32 | init: function (id) { 33 | var _this = this; 34 | 35 | $(function () { 36 | 37 | /* -- _this.options gives us access to the $jsonVars that our FieldType passed down to us */ 38 | 39 | }); 40 | } 41 | }; 42 | 43 | // A really lightweight plugin wrapper around the constructor, 44 | // preventing against multiple instantiations 45 | $.fn[pluginName] = function (options) { 46 | return this.each(function () { 47 | if (!$.data(this, "plugin_" + pluginName)) { 48 | $.data(this, "plugin_" + pluginName, 49 | new Plugin(this, options)); 50 | } 51 | }); 52 | }; 53 | 54 | })(jQuery, window, document); 55 | -------------------------------------------------------------------------------- /src/assetbundles/units/dist/js/Units.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Units plugin for Craft CMS 3 | * 4 | * Units JS 5 | * 6 | * @author nystudio107 7 | * @copyright Copyright (c) nystudio107 8 | * @link https://nystudio107.com/ 9 | * @package Units 10 | * @since 1.0.0 11 | */ 12 | 13 | /** 14 | * Fill a dynamic schema.org type menu with the units data 15 | * 16 | * @param menuId 17 | * @param menuValue 18 | * @param unitsClass 19 | * @param callback 20 | */ 21 | function fillDynamicUnitsMenu(menuId, menuValue, unitsClass, callback) { 22 | var menu = $('#' + menuId); 23 | 24 | if (menu.length) { 25 | menu.empty(); 26 | $.ajax({ 27 | url: Craft.getActionUrl('units/units/available-units?unitsClass=' + unitsClass) 28 | }) 29 | .done(function (data) { 30 | var newValue = Object.keys(data)[0]; 31 | for (var k in data) { 32 | if (data.hasOwnProperty(k)) { 33 | if (k === menuValue) { 34 | newValue = menuValue; 35 | } 36 | $('