{# "Edit Element" layout template The following variables should be defined by the sub-template: - element: the source element or one of its drafts/revisions - canDeleteDraft (optional): whether the current user is allowed to delete the draft (if it is one). If the current user created the draft, then it will be deletable regardless. - canUpdateSource (optional): whether the current user is allowed to update the source element (e.g. by publishing a draft or reverting the element to a prior revision) - canDuplicateSource: whether the current user is allowed to duplicate the source element - canAddAnother: whether the current user is allowed to create another source element after saving the current one - canDeleteSource: whether the current user is allowed to delete the source element - redirectUrl: the URL that the user should be redirected to after saving the source element - addAnotherRedirectUrl: the URL that the user should be redirected to after opting to save and add another - saveSourceAction: the controller action that should be used to save the source element - duplicateSourceAction: the controller action that should be used to duplicate the source element - deleteSourceAction: the controller action that should be used to delete the source element - saveDraftAction: the controller action that should be used to save a draft - deleteDraftAction: the controller action that should be used to delete a draft - applyDraftAction: the controller action that should be used to apply a draft onto the source element - revertSourceAction: the controller action that should be used to revert the source element to a revision - showStatusToggles: whether the “Enabled” / “Enabled” for ” fields should be added to the details pane #} {% extends '_layouts/cp' %} {% import '_includes/forms' as forms %} {% set supportedSiteIds = (craft.app.isMultiSite ? element.getSupportedSites() : [element.siteId])|column(s => s.siteId ?? s) %} {% set editableSiteIds = supportedSiteIds|intersect(craft.app.sites.getEditableSiteIds()) %} {% set isMultiSiteElement = craft.app.isMultiSite and supportedSiteIds|length > 1 %} {% set canEditMultipleSites = isMultiSiteElement and editableSiteIds|length > 1 %} {% set isDraft = element.getIsDraft() %} {% set isUnsavedDraft = element.getIsUnsavedDraft() %} {% set isRevision = element.getIsRevision() %} {% set isCurrent = not isDraft and not isRevision %} {% set previewTargets = element.getPreviewTargets() %} {% set enablePreview = previewTargets and not craft.app.request.isMobileBrowser(true) %} {% set canDeleteDraft = isDraft and not isUnsavedDraft and ((canDeleteDraft ?? false) or element.creatorId == currentUser.id) %} {% set canUpdateSource = canUpdateSource ?? false %} {% set canDuplicateSource = canDuplicateSource ?? false %} {% set canAddAnother = canAddAnother ?? false %} {% set canDeleteSource = canDeleteSource ?? false %} {% set canEdit = canEdit ?? (canUpdateSource or canDuplicateSource or canAddAnother or saveDraftAction) %} {% set redirectUrl = redirectUrl ?? element.cpEditUrl %} {% set addAnotherRedirectUrl = addAnotherRedirectUrl ?? null %} {% set saveSourceAction = saveSourceAction ?? null %} {% set duplicateSourceAction = duplicateSourceAction ?? null %} {% set deleteSourceAction = deleteSourceAction ?? null %} {% set revertSourceAction = revertSourceAction ?? null %} {% set saveDraftAction = saveDraftAction ?? null %} {% if not isRevision %} {% set fullPageForm = true %} {% endif %} {% if isDraft %} {% do craft.app.session.authorize('previewDraft:' ~ element.draftId) %} {% elseif isRevision %} {% do craft.app.session.authorize('previewRevision:' ~ element.revisionId) %} {% else %} {% do craft.app.session.authorize('previewElement:' ~ element.id) %} {% endif %} {# If this is an unsaved draft, then we should only show status toggles if the user actually has permission to publish chanegs #} {% set showStatusToggles = element.hasStatuses() and (not isUnsavedDraft or canUpdateSource) and (showStatusToggles ?? true) %} {% if not isDraft and not canUpdateSource %} {% set saveShortcut = false %} {% elseif isUnsavedDraft or (isCurrent and canUpdateSource) %} {% set saveShortcutRedirect = '{cpEditUrl}' %} {% endif %} {% if tabs is not defined %} {% set tabs = [] %} {% for tab in element.fieldLayout.tabs ?? [] %} {% set tabs = tabs|merge([{ label: tab.name|t('site'), url: '#' ~ tab.getHtmlId(), class: tab.elementHasErrors(element) ? 'error', }]) %} {% endfor %} {% endif %} {% set settingsHtml = (block('settings') ?? '')|trim %} {% block header %}
{{ block('pageTitle') }} {{ block('contextMenu') }}
{% if previewTargets %}
{% if enablePreview %}
{{ "Preview"|t('app') }}
{% endif %}
{{ 'Share'|t('app') }}
{% endif %} {% if isCurrent and saveDraftAction %}
{% if canUpdateSource and saveSourceAction %}
{{ 'Save as a draft'|t('app') }}
{% else %} {% endif %}
{% endif %} {{ block('actionButton') }}
{% endblock %} {% block contextMenu %} {% if isMultiSiteElement or saveDraftAction or element.find().revisionOf(element).exists() %} {% include "_includes/revisionmenu" with { canHaveDrafts: saveDraftAction is not empty, } %} {% endif %} {% endblock %} {% block actionButton %} {% if isUnsavedDraft or isCurrent %} {# Can they save the source element, and do we know how to save it? #} {% if isUnsavedDraft or (canUpdateSource and saveSourceAction) %}
{{ tag('input', { type: 'submit', class: ['btn', 'submit'], value: isUnsavedDraft ? 'Create'|t('app') : 'Save'|t('app'), }) }}
{% endif %} {% elseif isDraft %} {% if canUpdateSource and saveSourceAction %} {% endif %} {% elseif isRevision %} {% if canUpdateSource and revertSourceAction %}
{{ csrfInput() }} {{ actionInput(revertSourceAction) }} {{ redirectInput('{cpEditUrl}') }} {{ hiddenInput('revisionId', element.revisionId) }}
{% endif %} {% endif %} {% endblock %} {% block main %} {% if fullPageForm %} {# action and redirect params #} {% if not isDraft and canUpdateSource and saveSourceAction %} {# current revision -- user can update source #} {{ actionInput(saveSourceAction) }} {{ redirectInput(redirectUrl) }} {% endif %} {# siteId param #} {% if craft.app.isMultiSite %} {{ hiddenInput('siteId', element.siteId) }} {% endif %} {# propagateAll param #} {% if isUnsavedDraft and craft.app.request.getQueryParam('fresh') %} {{ hiddenInput('propagateAll', '1') }} {% endif %} {% endif %} {{ parent() }} {% endblock %} {% block content %}
{% for tab in element.fieldLayout.tabs ?? [] %} {% endfor %}
{% endblock %} {% block details %} {% if settingsHtml %}
{{ settingsHtml|raw }}
{% endif %} {% if showStatusToggles and isMultiSiteElement %}
{{ forms.lightswitchField({ status: element.getAttributeStatus('enabled'), label: 'Enabled for {site}'|t('app', { site: element.site.name|t('site')|e }) ~ (canEditMultipleSites ? ''), id: "enabledForSite-#{element.siteId}", name: "enabledForSite[#{element.siteId}]", on: element.enabled and element.enabledForSite, disabled: isRevision, }) }}
{% endif %}
{% block meta %} {% if element.hasStatuses() %} {% if isUnsavedDraft %} {% set statusColor = 'white' %} {% set statusLabel = 'Draft'|t('app') %} {% else %} {% set status = element.getStatus() %} {% set statusDef = element.statuses()[status] ?? null %} {% set statusColor = statusDef.color ?? status %} {% set statusLabel = statusDef.label ?? statusDef ?? status|ucfirst %} {% endif %}
{{ 'Status'|t('app') }}
{{ statusLabel }}
{% endif %}
{{ "Created at"|t('app') }}
{{ element.dateCreated|datetime('short') }}
{{ "Updated at"|t('app') }}
{{ element.dateUpdated|datetime('short') }}
{% if isRevision %} {% set revisionNotes = element.revisionNotes %} {% elseif not isDraft and element.currentRevision %} {% set revisionNotes = element.currentRevision.revisionNotes %} {% else %} {% set revisionNotes = null %} {% endif %} {% if revisionNotes %}
{{ "Notes"|t('app') }}
{{ revisionNotes }}
{% endif %} {% endblock %}
{% if isDraft and element.getIsOutdated() %} {% do craft.app.session.authorize('mergeDraftSourceChanges:' ~ element.draftId) %}

{{ 'The source {type} has been updated recently.'|t('app', {type: element.lowerDisplayName()}) }}

{% if element.trackChanges %} {{ tag('div', { id: 'merge-changes-btn', class: ['btn'], role: 'button', text: 'Merge changes into draft'|t('app'), }) }} {% endif %}
{% endif %} {% endblock %} {% block settings %} {% if showStatusToggles and not isMultiSiteElement %} {{ forms.lightswitchField({ status: element.getAttributeStatus('enabled'), label: 'Enabled'|t('app'), id: 'enabled', name: 'enabled', on: element.enabled, disabled: isRevision, }) }} {% endif %} {% endblock %} {% if canEditMultipleSites and element.enabled %} {# get the element's statuses across all sites #} {% set siteStatusesQuery = element.find() .select(['elements_sites.siteId', 'elements_sites.enabled']) .id(element.id) .siteId(editableSiteIds) .anyStatus() .asArray() %} {% if isDraft %} {% do siteStatusesQuery.drafts() %} {% elseif isRevision %} {% do siteStatusesQuery.revisions() %} {% endif %} {% set siteStatuses = siteStatusesQuery .pairs()|map(s => s ? true : false) %} {% elseif canEditMultipleSites %} {% set siteStatusValues = {} %} {% for siteId in editableSiteIds %} {% set siteStatusValues = siteStatusValues|merge([false]) %} {% endfor %} {% set siteStatuses = combine(editableSiteIds, siteStatusValues) %} {% else %} {% set siteStatuses = { ("#{element.siteId}"): element.enabled ? true : false } %} {% endif %} {% set settings = { elementType: className(element), elementTypeDisplayName: element.displayName(), sourceId: element.getSourceId(), siteId: element.siteId, isUnsavedDraft: isUnsavedDraft, siteStatuses: siteStatuses, enabled: element.enabled ? true : false, enabledForSite: element.enabled and element.enabledForSite, isLive: isCurrent and element.enabled and element.enabledForSite and element.getRoute(), cpEditUrl: element.cpEditUrl, hashedRedirectUrl: (isUnsavedDraft ? redirectUrl : '{cpEditUrl}')|hash, draftId: element.draftId, revisionId: element.revisionId, draftName: isDraft ? element.draftName : null, draftNotes: isDraft ? element.draftNotes : null, canEditMultipleSites: canEditMultipleSites, canDeleteDraft: canDeleteDraft, canUpdateSource: canUpdateSource, saveDraftAction: saveDraftAction, deleteDraftAction: deleteDraftAction ?? null, applyDraftAction: applyDraftAction ?? null, enablePreview: enablePreview, previewTargets: previewTargets, } %} {% js %} window.draftEditor = new Craft.DraftEditor({{ settings|json_encode|raw }}); {% endjs %}