Skip to content

Commit

Permalink
Merge pull request #16429 from craftcms/feature/quick-post-custom-title
Browse files Browse the repository at this point in the history
Widget Title setting for Quick Post widgets
  • Loading branch information
brandonkelly authored Jan 14, 2025
2 parents a451076 + 2c97645 commit 110a21f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Content Management
- It’s now possible to copy custom field values from other sites. ([#14056](https://github.com/craftcms/cms/pull/14056))
- “Related To”, “Not Related To”, “Author”, and relational field condition rules now allow multiple elements to be specified. ([#16121](https://github.com/craftcms/cms/discussions/16121))
- Added the “Widget Title” setting to Quick Post widgets. ([#16429](https://github.com/craftcms/cms/pull/16429))
- Improved the styling of inline code fragments. ([#16141](https://github.com/craftcms/cms/pull/16141))
- Improved the styling of attribute previews in card view. ([#16324](https://github.com/craftcms/cms/pull/16324))
- Added the “Affiliated Site” user condition rule. ([#16174](https://github.com/craftcms/cms/pull/16174))
Expand Down
49 changes: 33 additions & 16 deletions src/templates/_components/widgets/QuickPost/settings.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% import "_includes/forms" as forms %}

{% if sections %}

{% if craft.app.getIsMultiSite() %}
{% set editableSites = craft.app.sites.getEditableSites() %}

Expand All @@ -24,48 +23,66 @@
{% endif %}

{% set sectionOptions = [] %}
{% for section in sections %}
{% set sectionOptions = sectionOptions|merge([{ label: section.name|t('site'), value: section.id }]) %}
{% for s in sections %}
{% set sectionOptions = sectionOptions|merge([{ label: s.name|t('site'), value: s.id }]) %}
{% endfor %}
{{ forms.selectField({
label: "Section"|t('app'),
instructions: 'Which section do you want to save entries to?'|t('app'),
id: 'section',
name: 'section',
options: sectionOptions,
value: sectionId,
value: section.id ?? null,
toggle: true,
targetPrefix: 'section'
}) }}

{% for section in sections %}
{% set showSection = ((not sectionId and loop.first) or sectionId == section.id) %}
<div id="section{{ section.id }}"{% if not showSection %} class="hidden"{% endif %}>
{% for s in sections %}
{% set showSection = ((not section and loop.first) or (section.id ?? null) == s.id) %}
<div id="section{{ s.id }}"{% if not showSection %} class="hidden"{% endif %}>

{% set entryTypeOptions = [] %}
{% for entryType in section.getEntryTypes() %}
{% set entryTypeOptions = entryTypeOptions|merge([{ label: entryType.name|t('site'), value: entryType.id }]) %}
{% for et in s.getEntryTypes() %}
{% set entryTypeOptions = entryTypeOptions|merge([{ label: et.name|t('site'), value: et.id }]) %}
{% endfor %}

{% if entryTypeOptions|length == 1 %}
{{ hiddenInput("sections[#{section.id}][entryType]", entryTypeId) }}
{{ hiddenInput("sections[#{s.id}][entryType]", entryType.id ?? null) }}
{% else %}
{{ forms.selectField({
label: "Entry Type"|t('app'),
instructions: "Which type of entries do you want to create?"|t('app'),
id: 'entryType',
name: 'sections['~section.id~'][entryType]',
name: 'sections['~s.id~'][entryType]',
options: entryTypeOptions,
value: entryTypeId,
value: entryType.id ?? null,
toggle: true,
targetPrefix: 'section'~section.id~'-type'
targetPrefix: 'section'~s.id~'-type'
}) }}
{% endif %}
</div>
{% endfor %}

{{ forms.textField({
label: 'Widget Title'|t('app'),
id: 'custom-title',
name: 'customTitle',
value: widget.customTitle,
placeholder: 'Create a new {section} entry'|t('app', {
section: (section ?? sections|first).getUiLabel(),
}),
}) }}
{% else %}

<p>{{ "No sections are available."|t('app') }}</p>

{% endif %}

{% js %}
(() => {
const $sectionSelect = $('#{{ 'section'|namespaceInputId }}');
const $titleInput = $('#{{ 'custom-title'|namespaceInputId }}');
$sectionSelect.on('change', () => {
$titleInput.attr('placeholder', Craft.t('app', 'Create a new {section} entry', {
section: $sectionSelect.find(`option[value="${$sectionSelect.val()}"]`).text(),
}));
});
})();
{% endjs %}
18 changes: 16 additions & 2 deletions src/widgets/QuickPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public static function icon(): ?string
*/
public ?int $entryType = null;

/**
* @var string|null The custom widget title.
* @since 5.6.0
*/
public ?string $customTitle = null;

/**
* @var Section|false
* @see section()
Expand Down Expand Up @@ -84,6 +90,10 @@ public function __construct($config = [])
unset($config['sections']);
}

if (isset($config['customTitle']) && $config['customTitle'] === '') {
unset($config['customTitle']);
}

unset($config['fields']);

parent::__construct($config);
Expand Down Expand Up @@ -120,8 +130,8 @@ public function getSettingsHtml(): ?string
'sections' => $sections,
'widget' => $this,
'siteId' => $this->siteId,
'sectionId' => $this->section()?->id,
'entryTypeId' => $this->entryType()?->id,
'section' => $this->section(),
'entryType' => $this->entryType(),
]);
}

Expand All @@ -130,6 +140,10 @@ public function getSettingsHtml(): ?string
*/
public function getTitle(): ?string
{
if (isset($this->customTitle)) {
return Craft::t('site', $this->customTitle);
}

$entryType = $this->entryType();
if (!$entryType) {
return static::displayName();
Expand Down

0 comments on commit 110a21f

Please sign in to comment.