Skip to content

Commit

Permalink
Add time field parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmacwhite committed Dec 29, 2024
1 parent f0c9691 commit 8316258
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/fields/Time.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace craft\feedme\fields;

use Cake\Utility\Hash;
use craft\feedme\base\Field;
use craft\feedme\base\FieldInterface;
use craft\feedme\helpers\DateHelper;
use craft\fields\Time as TimeField;
use craft\helpers\DateTimeHelper;
use DateTime;

/**
*
* @property-read string $mappingTemplate
*/
class Time extends Field implements FieldInterface
{
// Properties
// =========================================================================

/**
* @var string
*/
public static string $name = 'Time';

/**
* @var string
*/
public static string $class = TimeField::class;

// Templates
// =========================================================================

/**
* @inheritDoc
*/
public function getMappingTemplate(): string
{
return 'feed-me/_includes/fields/time';
}

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

/**
* @inheritDoc
*/
public function parseField(): mixed
{
$value = $this->fetchValue();

if ($value === null) {
return null;
}

$timeValue = DateHelper::parseTimeString($value);

if ($timeValue) {
return $timeValue;
}

return $value;
}
}
2 changes: 2 additions & 0 deletions src/services/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use craft\feedme\fields\SuperTable;
use craft\feedme\fields\Table;
use craft\feedme\fields\Tags;
use craft\feedme\fields\Time;
use craft\feedme\fields\TypedLink;
use craft\feedme\fields\Users;
use craft\helpers\Component as ComponentHelper;
Expand Down Expand Up @@ -142,6 +143,7 @@ public function getRegisteredFields(): array
RadioButtons::class,
Table::class,
Tags::class,
Time::class,
Users::class,

// Third-Party
Expand Down
8 changes: 8 additions & 0 deletions src/templates/_includes/fields/time.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% import 'feed-me/_macros' as feedMeMacro %}
{% import '_includes/forms' as forms %}

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

{% extends 'feed-me/_includes/fields/_base' %}

0 comments on commit 8316258

Please sign in to comment.