forked from studioespresso/craft3-seeder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
populate certain fields directly via console or CP
- Loading branch information
1 parent
2c2b8b9
commit 1129b91
Showing
8 changed files
with
241 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* Craft CMS Plugins | ||
* | ||
* Created with PhpStorm. | ||
* | ||
* @link https://github.com/Anubarak/ | ||
* @email [email protected] | ||
* @copyright Copyright (c) 2024 Robin Schambach|Secondred Newmedia GmbH | ||
*/ | ||
|
||
namespace anubarak\seeder\console\controllers; | ||
|
||
use anubarak\seeder\Seeder; | ||
use craft\console\Controller; | ||
use craft\helpers\ArrayHelper; | ||
use craft\helpers\Db; | ||
use yii\console\ExitCode; | ||
|
||
/** | ||
* Class PopulateController | ||
* | ||
* @package anubarak\seeder\console\controllers | ||
* @since 08.07.2024 | ||
* @author by Robin Schambach | ||
*/ | ||
class PopulateController extends Controller | ||
{ | ||
/** | ||
* comma separated fields that should be populated | ||
* | ||
* @var string|null $fields | ||
*/ | ||
public ?string $fields = null; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function options($actionID): array | ||
{ | ||
$options = parent::options($actionID); | ||
if ($actionID === 'index') { | ||
$options[] = 'fields'; | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* populate an element by it's ID, optionally pass a comma separated list of fields | ||
* | ||
* @param int $id | ||
* | ||
* @return int|void | ||
* @throws \yii\base\ExitException | ||
* @throws \yii\base\InvalidConfigException | ||
* @throws \yii\base\NotSupportedException | ||
* @author Robin Schambach | ||
* @since 08.07.2024 | ||
*/ | ||
public function actionIndex(int $id) | ||
{ | ||
$element = \Craft::$app->getElements()->getElementById($id); | ||
if (!$element) { | ||
$this->stderr('No element found with ID: ' . $id . PHP_EOL); | ||
|
||
return ExitCode::UNSPECIFIED_ERROR; | ||
} | ||
|
||
$fields = []; | ||
if ($this->fields) { | ||
$fields = ArrayHelper::toArray($this->fields); | ||
} | ||
|
||
$seeder = Seeder::$plugin->getSeeder(); | ||
$seeder->populateFields($element, $fields); | ||
if (!\Craft::$app->getElements()->saveElement($element)) { | ||
$this->stderr('Failed to save element with ID: ' . $id . PHP_EOL); | ||
|
||
return ExitCode::UNSPECIFIED_ERROR; | ||
} | ||
|
||
return ExitCode::OK; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{% import '_includes/forms' as forms %} | ||
|
||
<input type="hidden" name="action" value="element-seeder/seeder/generate-content"> | ||
<input type="hidden" name="elementId" value="{{ elementId }}"> | ||
{% for data in fieldData %} | ||
<div class="field"> | ||
<h2>{{ data.tab }}</h2> | ||
<div> | ||
{% for field in data.fields %} | ||
{{ forms.lightSwitchField({ | ||
label: field.label(), | ||
name: 'fields[' ~ field.attribute() ~ ']', | ||
}) }} | ||
{% endfor %} | ||
</div> | ||
</div> | ||
<hr> | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters