Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SFT-1656): neo fields (v4) #357

Open
wants to merge 1 commit into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 27 additions & 53 deletions packages/plugin/src/Elements/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use craft\i18n\Locale;
use craft\models\FieldLayout;
use craft\models\FieldLayoutTab;
use craft\web\UploadedFile;
use Illuminate\Support\Collection;
use RRule\RRule;
use Solspace\Calendar\Calendar;
Expand Down Expand Up @@ -146,8 +145,6 @@ class Event extends Element implements \JsonSerializable
/** @var Event[] */
private $occurrenceCache = [];

private ?string $_fieldParamNamePrefix = null;

/**
* Event constructor.
*/
Expand Down Expand Up @@ -1323,6 +1320,11 @@ public function diffInDays(self $event): int
return DateHelper::carbonDiffInDays($this->getStartDate(), $event->getStartDate());
}

public function canDuplicate(User $user): bool
{
return $this->isEditable($this);
}

public function canDelete(User $user): bool
{
return $this->isEditable($this);
Expand Down Expand Up @@ -1615,65 +1617,37 @@ public static function actions(string $source): array
return $event->actions;
}

public function setFieldValuesFromRequest(string $paramNamespace = ''): void
public function attributes(): array
{
$this->setFieldParamNamespace($paramNamespace);
$names = parent::attributes();
$names[] = 'authorId';
$names[] = 'author';

if (isset($this->_fieldParamNamePrefix)) {
$values = \Craft::$app->getRequest()->getBodyParam($paramNamespace, []);
} else {
$values = \Craft::$app->getRequest()->getBodyParams();
// Hide Author from Craft Solo
if (\Craft::Solo === \Craft::$app->getEdition()) {
unset($names['authorId'], $names['author']);
}

// Run through this multiple times, in case any fields become visible as a result of other field value changes
$processedFields = [];
do {
$processedAnyFields = false;
foreach ($this->fieldLayoutFields(true) as $field) {
// Have we already processed this field?
if (isset($processedFields[$field->id])) {
continue;
}
return $names;
}

$processedFields[$field->id] = true;
$processedAnyFields = true;

// Do we have any post data for this field?
if (isset($values[$field->handle])) {
$value = $values[$field->handle];
} elseif (
isset($this->_fieldParamNamePrefix)
&& UploadedFile::getInstancesByName("{$this->_fieldParamNamePrefix}.{$field->handle}")
) {
// A file was uploaded for this field
$value = null;
} else {
continue;
}
public function extraFields(): array
{
$names = parent::extraFields();
$names[] = 'authorId';
$names[] = 'author';

// Add in additional support for other field types
if ($field instanceof \benf\neo\Field) {
if (!empty($values[$field->handle]['blocks']) && \is_array($values[$field->handle]['blocks'])) {
$index = 0;
$blocks = [];
foreach ($values[$field->handle]['blocks'] as $block) {
$blocks['new'.$index] = $block;
++$index;
}
$this->setFieldValues([$field->handle => $blocks]);
} else {
$this->setFieldValues([$field->handle => '']);
}
} else {
$this->setFieldValueFromRequest($field->handle, $value);
}
}
} while ($processedAnyFields);
// Hide Author from Craft Solo
if (\Craft::Solo === \Craft::$app->getEdition()) {
unset($names['authorId'], $names['author']);
}

return $names;
}

public function setFieldParamNamespace(string $namespace): void
protected static function prepElementQueryForTableAttribute(ElementQueryInterface $elementQuery, string $attribute): void
{
$this->_fieldParamNamePrefix = '' !== $namespace ? $namespace : null;
parent::prepElementQueryForTableAttribute($elementQuery, $attribute);
}

protected static function defineSources(string $context = null): array
Expand Down
27 changes: 10 additions & 17 deletions packages/plugin/src/templates/events/_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{% set calendarHandle = calendar.handle|default %}
{% set selectedSubnavItem = 'events' %}
{% set saveShortcutRedirect = continueEditingUrl %}
{% set elementType = 'Solspace\\Calendar\\Elements\\Event' %}

{% set crumbs = [
{ label: craft.calendar.name, url: url('calendar') },
Expand Down Expand Up @@ -58,31 +59,19 @@
<div class="menu">
<ul>
<li>
<a class="formsubmit" data-redirect="{{ continueEditingUrl|hash }}">
{{ "Save and continue editing"|t('calendar') }}
<span class="shortcut">⌘S</span>
<a class="formsubmit" data-redirect="{{ continueEditingUrl|hash }}">{{ "Save and continue editing"|t('calendar') }}<span class="shortcut">⌘S</span>
</a>
</li>
<li>
<a class="formsubmit"
data-redirect="{{ ('calendar/events/new/'~calendarHandle~'/'~site.handle)|hash }}">
{{ "Save and add another"|t('calendar') }}
<a class="formsubmit" data-redirect="{{ ('calendar/events/new/'~calendarHandle~'/'~site.handle)|hash }}">{{ "Save and add another"|t('calendar') }}
</a>
</li>
{% if event.id %}
<li><a class="formsubmit" data-param="eventId"
data-value=""
data-redirect="{{ continueEditingUrl|hash }}">{{ "Save as a new event"|t('calendar') }}</a>
<li><a class="formsubmit" data-action="elements/duplicate" data-redirect="{{ continueEditingUrl|hash }}">{{ "Save as a new event"|t('calendar') }}</a>
</li>
<hr>
<li>
<a class="formsubmit error"
data-action="calendar/events/delete-event"
data-confirm="{{ 'Are you sure you want to delete this event?'|t('calendar') }}"
data-redirect="{{ 'calendar/events'|hash }}"
>
{{ "Delete"|t('calendar') }}
</a>
<a class="formsubmit error" data-action="calendar/events/delete-event" data-confirm="{{ 'Are you sure you want to delete this event?'|t('calendar') }}" data-redirect="{{ 'calendar/events'|hash }}">{{ "Delete"|t('calendar') }}</a>
</li>
{% endif %}
</ul>
Expand All @@ -97,8 +86,12 @@
{{ redirectInput('calendar/events') }}
{{ csrfInput() }}
{% if craft.app.isMultiSite %}<input type="hidden" name="siteId" value="{{ event.siteId }}">{% endif %}
{% if event.id %}<input type="hidden" name="eventId" value="{{ event.id }}">{% endif %}
{% if event.id %}
<input type="hidden" name="eventId" value="{{ event.id }}">
<input type="hidden" name="elementId" value="{{ event.id }}">
{% endif %}
<input type="hidden" name="{{ name ~ '[calendarId]' }}" value="{{ event.calendarId }}">
<input type="hidden" name="elementType" value="{{ elementType }}">

{% include "calendar/events/_event_editor" %}

Expand Down