Skip to content

Commit

Permalink
Added Twig extension to display coordinates in front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
niektenhoopen committed Aug 31, 2019
1 parent d6ac258 commit c6d9e16
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 3 deletions.
18 changes: 16 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.1 - 2019-08-31
### Added
- Added installation instructions to readme
- Added Twig filter to output coordinates in front-end
- Added Twig documentation to readme


## 1.0.0 - 2019-08-31
### Added
- Initial release
### Added
- First version of the Craft plugin.
- Include plugin icon
- Include Craft license
- Add preview to readme (#1)
- Draggable marker on a Google Map
- Includes searching through Google Places API
- Google key configurable in field setting

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,22 @@ Pick a GPS location for an entry

![PluginImpression](https://user-images.githubusercontent.com/3450011/64065265-e3cbc680-cc0b-11e9-89fc-ed682123b109.gif)

## Installation

To install the plugin, follow these instructions.

1. Open your terminal and go to your Craft project and tell composer to install the plugin
```composer require nthmedia```

2. Activate the plugin in Craft through the command line or through the Control Panel under Settings → Plugins
```./craft install/plugin entry-gps-coordinates```

3. Add a Coordinates field under Settings → Fields and add this field to a Section under Settings → Sections

## Usage in Twig

```
{{ entry.fieldName | coordinates }} => 47.11736635136284,12.82762888348384
{{ entry.fieldName | latitude }} => 47.11736635136284
{{ entry.fieldName | longitude }} => 12.82762888348384
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nthmedia/entry-gps-coordinates",
"description": "Select a GPS location for an entry",
"description": "Entry GPS Coordinates plugin for Craft CMS 3.x",
"type": "craft-plugin",
"version": "1.0.0",
"keywords": [
Expand Down
4 changes: 4 additions & 0 deletions src/EntryGpsCoordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace nthmedia\entrygpscoordinates;

use nthmedia\entrygpscoordinates\fields\EntryCoordinates as EntryCoordinatesField;
use nthmedia\entrygpscoordinates\twigextensions\EntryGpsCoordinatesTwigExtension;

use Craft;
use craft\base\Plugin;
Expand Down Expand Up @@ -78,6 +79,9 @@ public function init()
parent::init();
self::$plugin = $this;

// Register Twig extension
Craft::$app->view->registerTwigExtension(new EntryGpsCoordinatesTwigExtension());

// Register our fields
Event::on(
Fields::class,
Expand Down
100 changes: 100 additions & 0 deletions src/twigextensions/EntryGpsCoordinatesTwigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Entry GPS Coordinates plugin for Craft CMS 3.x
*
* Pick a GPS location for an entry
*
* @link https://nthmedia.nl
* @copyright Copyright (c) 2019 NTH media
*/

namespace nthmedia\entrygpscoordinates\twigextensions;

use nthmedia\entrygpscoordinates\EntryGpsCoordinates;

use Craft;

/**
* @author nthmedia
* @package EntryGpsCoordinates
* @since 1
*/
class EntryGpsCoordinatesTwigExtension extends \Twig\Extension\AbstractExtension
{
// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function getName()
{
return 'Entry GPS Coordinates';
}

/**
* @inheritdoc
*/
public function getFilters()
{
return [
new \Twig\TwigFilter('coordinates', [$this, 'coordinates']),
new \Twig\TwigFilter('latitude', [$this, 'latitude']),
new \Twig\TwigFilter('longitude', [$this, 'longitude']),
];
}

/**
* @inheritdoc
*/
public function getFunctions()
{
return [
new \Twig\TwigFunction('coordinates', [$this, 'coordinates']),
new \Twig\TwigFunction('latitude', [$this, 'latitude']),
new \Twig\TwigFunction('longitude', [$this, 'longitude']),
];
}

/**
* @param null $value
*
* @return string
*/
public function coordinates($value = null)
{
if (array_key_exists('coordinates', $value)) {
return $value['coordinates'];
}

return $value;
}

/**
* @param null $value
*
* @return string
*/
public function latitude($value = null)
{
if (array_key_exists('coordinates', $value) && preg_match('/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/', $value['coordinates'], $matches)) {
return $matches[1];
}

return $value;
}

/**
* @param null $value
*
* @return string
*/
public function longitude($value = null)
{
if (array_key_exists('coordinates', $value) && preg_match('/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/', $value['coordinates'], $matches)) {
return $matches[4];
}

return $value;
}
}

0 comments on commit c6d9e16

Please sign in to comment.