Skip to content

Commit

Permalink
Add mock elements for all supported element types
Browse files Browse the repository at this point in the history
  • Loading branch information
Mosnar committed Feb 20, 2020
1 parent bba0fcf commit 42f57a3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Bulk Edit Changelog

## Unreleased
### Added
- All field types (including custom ones and Matrix) now support bulk replacement!!!

## 2.0.1 - 2020-02-13
### Fixed
- Fixed problem with saving bulk edit jobs in Firefox
Expand Down
14 changes: 13 additions & 1 deletion src/elements/processors/AssetProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace venveo\bulkedit\elements\processors;

use Craft;
use craft\base\Element;
use craft\elements\Asset;
use craft\helpers\ArrayHelper;
use craft\records\FieldLayout;
Expand Down Expand Up @@ -39,7 +41,7 @@ public static function getLayoutsFromElementIds($elementIds): array
*/
public static function getType(): string
{
return get_class(new Asset);
return Asset::class;
}

/**
Expand All @@ -52,4 +54,14 @@ public static function hasPermission($elementIds, User $user): bool
{
return $user->checkPermission(Plugin::PERMISSION_BULKEDIT_ASSETS);
}

public static function getMockElement($elementIds = [], $params = []): Element
{
$asset = Craft::$app->assets->getAssetById($elementIds[0]);
/** @var Asset $elementPlaceholder */
$elementPlaceholder = Craft::createObject(static::getType(), $params);
// Field availability is determined by volume ID
$elementPlaceholder->volumeId = $asset->volumeId;
return $elementPlaceholder;
}
}
14 changes: 13 additions & 1 deletion src/elements/processors/CategoryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace venveo\bulkedit\elements\processors;

use Craft;
use craft\base\Element;
use craft\elements\Category;
use craft\helpers\ArrayHelper;
use craft\records\CategoryGroup;
Expand Down Expand Up @@ -48,7 +50,7 @@ public static function getLayoutsFromElementIds($elementIds): array
*/
public static function getType(): string
{
return get_class(new Category);
return Category::class;
}

/**
Expand All @@ -61,4 +63,14 @@ public static function hasPermission($elementIds, User $user): bool
{
return $user->checkPermission(Plugin::PERMISSION_BULKEDIT_CATEGORIES);
}

public static function getMockElement($elementIds = [], $params = []): Element
{
$category = Craft::$app->categories->getCategoryById($elementIds[0]);
/** @var Category $elementPlaceholder */
$elementPlaceholder = Craft::createObject(static::getType(), $params);
// Field availability is determined by volume ID
$elementPlaceholder->groupId = $category->groupId;
return $elementPlaceholder;
}
}
21 changes: 11 additions & 10 deletions src/elements/processors/EntryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function getLayoutsFromElementIds($elementIds): array
*/
public static function getType(): string
{
return get_class(new Entry);
return Entry::class;
}

/**
Expand All @@ -57,15 +57,16 @@ public static function hasPermission($elementIds, User $user): bool
* @return array
*/
public static function getEditableAttributes(): array {
return [
[
'name' => 'Title',
'handle' => 'title',
'strategies' => [
BulkEdit::STRATEGY_REPLACE
]
]
];
// return [
// [
// 'name' => 'Title',
// 'handle' => 'title',
// 'strategies' => [
// BulkEdit::STRATEGY_REPLACE
// ]
// ]
// ];
return [];
}

/**
Expand Down
15 changes: 14 additions & 1 deletion src/elements/processors/ProductProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace venveo\bulkedit\elements\processors;

use Craft;
use craft\base\Element;
use craft\commerce\records\Product;
use craft\commerce\records\ProductType;
use craft\helpers\ArrayHelper;
Expand Down Expand Up @@ -48,7 +50,7 @@ public static function getLayoutsFromElementIds($elementIds): array
*/
public static function getType(): string
{
return get_class(new \craft\commerce\elements\Product);
return \craft\commerce\elements\Product::class;
}

/**
Expand All @@ -61,4 +63,15 @@ public static function hasPermission($elementIds, User $user): bool
{
return $user->checkPermission(Plugin::PERMISSION_BULKEDIT_PRODUCTS);
}

public static function getMockElement($elementIds = [], $params = []): Element
{
/** @var \craft\commerce\elements\Product $product */
$product = \Craft::$app->elements->getElementById($elementIds[0], \craft\commerce\elements\Product::class);
/** @var \craft\commerce\elements\Product $elementPlaceholder */
$elementPlaceholder = Craft::createObject(static::getType(), $params);
// Field availability is determined by volume ID
$elementPlaceholder->typeId = $product->typeId;
return $elementPlaceholder;
}
}
2 changes: 1 addition & 1 deletion src/elements/processors/UserProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function getLayoutsFromElementIds($elementIds): array
*/
public static function getType(): string
{
return get_class(new User);
return User::class;
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/templates/elementactions/BulkEdit/_fields.twig
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@
{% endif %}
{% else %}
<p>These elements have no associated fields.</p>
<div class="field-edit-modal-bar">
<div class="buttons right">
<div class="btn" id="field-edit-cancel">Cancel</div>
</div>
</div>
{% endif %}

{% if attributeWrappers|length %}
Expand Down

0 comments on commit 42f57a3

Please sign in to comment.