Skip to content

Commit

Permalink
Add Feed Me integration
Browse files Browse the repository at this point in the history
  • Loading branch information
rynpsc committed Sep 20, 2023
1 parent e83940f commit 8537394
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@

use rynpsc\phonenumber\fields\PhoneNumberField;
use rynpsc\phonenumber\gql\types\PhoneNumberType;
use rynpsc\phonenumber\integrations\feedme\Field as FeedMeField;
use rynpsc\phonenumber\services\PhoneNumber as PhoneNumberService;
use rynpsc\phonenumber\twigextensions\PhoneNumberExtension;

use Craft;
use craft\base\Plugin;
use craft\feedme\Plugin as FeedMe;
use craft\feedme\events\RegisterFeedMeFieldsEvent;
use craft\feedme\services\Fields as FeedMeFields;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterGqlTypesEvent;
use craft\services\Fields;
Expand Down Expand Up @@ -76,6 +80,16 @@ function(RegisterGqlTypesEvent $event) {
}
);

if (class_exists(FeedMe::class)) {
Event::on(
FeedMeFields::class,
FeedMeFields::EVENT_REGISTER_FEED_ME_FIELDS,
function(RegisterFeedMeFieldsEvent $event) {
$event->fields[] = FeedMeField::class;
}
);
}

if (Craft::$app->getRequest()->getIsSiteRequest()) {
Craft::$app->getView()->registerTwigExtension(new PhoneNumberExtension());
}
Expand Down
44 changes: 44 additions & 0 deletions src/integrations/feedme/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace rynpsc\phonenumber\integrations\feedme;

use Cake\Utility\Hash;
use craft\feedme\base\Field as BaseField;
use craft\feedme\base\FieldInterface;
use craft\feedme\helpers\DataHelper;
use rynpsc\phonenumber\fields\PhoneNumberField;

class Field extends BaseField implements FieldInterface
{
public static string $name = 'Phone Number';

public static string $class = PhoneNumberField::class;

/**
* @inheritdoc
*/
public function getMappingTemplate(): string
{
return 'phone-number/integrations/feedme/mapping-template';
}

/**
* @inheritdoc
*/
public function parseField(): mixed
{
$preppedData = [];

$fields = Hash::get($this->fieldInfo, 'fields');

if (!$fields) {
return null;
}

foreach ($fields as $subFieldHandle => $subFieldInfo) {
$preppedData[$subFieldHandle] = DataHelper::fetchValue($this->feedData, $subFieldInfo, $this->feed);
}

return $preppedData;
}
}
41 changes: 41 additions & 0 deletions src/templates/integrations/feedme/mapping-template.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% if parentPath is defined %}
{% set prefixPath = parentPath %}
{% else %}
{% set prefixPath = [handle] %}
{% endif %}

{% set classes = ['complex-field'] %}

<tr class="complex-field complex-field-header">
<td class="col-field" colspan="3">
<div class="field">
<div class="heading">
<label class="">{{ name }}</label>
</div>

<div class="additional-mapping-fields">
{% namespace 'fieldMapping[' ~ prefixPath | join('][') ~ ']' %}
<input type="hidden" name="field" value="{{ className(field) }}">
{% endnamespace %}
</div>
</div>
</td>
</tr>

{% set fields = {
region: 'Region',
number: 'Number',
} %}

{% for key, col in fields %}
{% set nameLabel = col %}
{% set instructionsHandle = handle ~ '[' ~ key ~ ']' %}

{% set path = prefixPath|merge ([ 'fields', key ]) %}

{% set default = default ?? {
type: 'text',
} %}

{% embed 'feed-me/_includes/fields/_base' %}{% endembed %}
{% endfor %}

0 comments on commit 8537394

Please sign in to comment.