Skip to content

Commit

Permalink
Post Object ACF adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Nov 4, 2024
1 parent 7263d23 commit 642133d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ ddev craft wp-import --help
- Link
- Number
- Page Link
- Post Object
- Radio Button
- Range
- Relationship
Expand Down
51 changes: 51 additions & 0 deletions src/acfadapters/PostObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/

namespace craft\wpimport\acfadapters;

use craft\base\FieldInterface;
use craft\fields\Entries;
use craft\wpimport\BaseAcfAdapter;
use craft\wpimport\importers\PostType;
use yii\console\Exception;

/**
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class PostObject extends BaseAcfAdapter
{
public static function type(): string
{
return 'post_object';
}

public function create(array $data): FieldInterface
{
$field = new Entries();
$field->sources = array_map(function(string $type) use ($data) {
$importer = $this->command->importers[$type] ?? null;
if (!$importer instanceof PostType) {
throw new Exception("Unsupported post type in the {$data['label']} field: $type");
}
return sprintf('section:%s', $importer->section()->uid);
}, $data['post_type']);
$field->maxRelations = 1;
return $field;
}

public function normalizeValue(mixed $value, array $data): mixed
{
if (count($data['post_type']) === 1) {
$type = reset($data['post_type']);
$id = $this->command->import($type, $value);
} else {
$id = $this->command->importPost($value);
}

return [$id];
}
}
4 changes: 2 additions & 2 deletions src/importers/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function populate(ElementInterface $element, array $data): void
}
}

private function entryType(): EntryType
public function entryType(): EntryType
{
if (isset($this->entryType)) {
return $this->entryType;
Expand Down Expand Up @@ -289,7 +289,7 @@ private function entryType(): EntryType
return $this->entryType = $entryType;
}

private function section(): Section
public function section(): Section
{
if (isset($this->section)) {
return $this->section;
Expand Down

0 comments on commit 642133d

Please sign in to comment.