Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
section_library
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
section_library
Commits
df0b5d26
Commit
df0b5d26
authored
1 month ago
by
Jess Straatmann
Browse files
Options
Downloads
Plain Diff
Merge branch '3228163-update-existing-template2' into '2.0.x'
Draft: Issue
#3228163
: Start of adding template update function in add form See merge request
!54
parents
0662bb95
0cba6ea6
No related branches found
No related tags found
No related merge requests found
Pipeline
#452809
passed with warnings
1 month ago
Stage: build
Stage: validate
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Form/AddSectionToLibraryForm.php
+85
-17
85 additions, 17 deletions
src/Form/AddSectionToLibraryForm.php
with
85 additions
and
17 deletions
src/Form/AddSectionToLibraryForm.php
+
85
−
17
View file @
df0b5d26
...
...
@@ -146,30 +146,87 @@ class AddSectionToLibraryForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
,
string
$template_type
=
'section'
,
?SectionStorageInterface
$section_storage
=
NULL
,
?int
$delta
=
NULL
)
{
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
,
string
$template_type
=
'section'
,
?SectionStorageInterface
$section_storage
=
NULL
,
?int
$delta
=
NULL
)
{
$this
->
sectionStorage
=
$section_storage
;
$this
->
delta
=
$delta
;
$this
->
templateType
=
$template_type
;
// @todo Need to filter based on user edit permission.
$existing_templates
=
$this
->
entityTypeManager
->
getStorage
(
'section_library_template'
)
->
loadByProperties
([
'type'
=>
$this
->
templateType
,
]);
// Use configured template type labels.
if
(
$this
->
templateType
===
'template'
)
{
$label
=
strtolower
(
$this
->
config
->
get
(
'template_label'
));
}
else
{
$label
=
strtolower
(
$this
->
config
->
get
(
'section_label'
));
}
$form
=
parent
::
buildForm
(
$form
,
$form_state
);
$form
[
'create_or_update'
]
=
[
'#type'
=>
'radios'
,
'#options'
=>
[
'create'
=>
$this
->
t
(
'Add :label'
,
[
':label'
=>
$label
]),
'update'
=>
$this
->
t
(
'Update existing :label'
,
[
':label'
=>
$label
]),
],
'#default_value'
=>
'create'
,
'#required'
=>
TRUE
,
'#access'
=>
count
(
$existing_templates
)
>
0
,
];
$label_state
=
count
(
$existing_templates
)
>
0
?
[
'visible'
=>
[
':input[name="create_or_update"]'
=>
[
'value'
=>
'create'
],
],
'required'
=>
[
':input[name="create_or_update"]'
=>
[
'value'
=>
'create'
],
],
]
:
[];
$existing_templates_options
=
[];
foreach
(
$existing_templates
as
$template_id
=>
$template
)
{
$existing_templates_options
[
$template_id
]
=
$template
->
label
();
}
$form
[
'existing_template'
]
=
[
'#type'
=>
'select'
,
'#title'
=>
$this
->
t
(
'Select template to update'
),
'#options'
=>
$existing_templates_options
,
'#access'
=>
count
(
$existing_templates
)
>
0
,
'#states'
=>
[
'visible'
=>
[
':input[name="create_or_update"]'
=>
[
'value'
=>
'update'
],
],
]
];
if
(
isset
(
$form
[
'label'
]))
{
$form
[
'label'
][
'#states'
]
=
$label_state
;
}
// @todo Need to hide custom fields if updating a template.
// or do something to, on select, populate the form fields
// from the existing template to be updated.
if
(
isset
(
$form
[
'actions'
][
'submit'
]))
{
// Use configured labels.
$sectionLabel
=
strtolower
(
$this
->
config
->
get
(
'section_label'
));
$templateLabel
=
strtolower
(
$this
->
config
->
get
(
'template_label'
));
if
(
$this
->
templateType
===
'template'
)
{
$form
[
'actions'
][
'submit'
][
'#value'
]
=
$this
->
t
(
'Add :templateLabel'
,
[
':templateLabel'
=>
$templateLabel
]);
}
else
{
$form
[
'actions'
][
'submit'
][
'#value'
]
=
$this
->
t
(
'Add :sectionLabel'
,
[
':sectionLabel'
=>
$sectionLabel
]);
}
$form
[
'actions'
][
'submit'
][
'#value'
]
=
$this
->
t
(
'Add/Update :label'
,
[
':label'
=>
$label
]);
if
(
$this
->
isAjax
())
{
$form
[
'actions'
][
'submit'
][
'#ajax'
][
'callback'
]
=
'::ajaxSubmit'
;
}
}
$form
[
'#attributes'
][
'data-layout-builder-target-highlight-id'
]
=
$this
->
sectionAddHighlightId
(
$delta
);
$form
[
'#attributes'
][
'data-layout-builder-target-highlight-id'
]
=
$this
->
sectionAddHighlightId
(
$delta
);
// Mark this as an administrative page for JavaScript ("Back to site" link).
$form
[
'#attached'
][
'drupalSettings'
][
'path'
][
'currentPathIsAdmin'
]
=
TRUE
;
...
...
@@ -180,9 +237,6 @@ class AddSectionToLibraryForm extends ContentEntityForm {
* {@inheritdoc}
*/
public
function
buildEntity
(
array
$form
,
FormStateInterface
$form_state
)
{
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity
=
parent
::
buildEntity
(
$form
,
$form_state
);
if
(
$this
->
templateType
===
'template'
)
{
$sections
=
$this
->
sectionStorage
->
getSections
();
$deep_cloned_section
=
$this
->
deepCloneSections
(
$sections
);
...
...
@@ -205,8 +259,22 @@ class AddSectionToLibraryForm extends ContentEntityForm {
throw
new
ContextException
(
''
,
0
,
$e
);
}
$entity
->
set
(
'layout_section'
,
$deep_cloned_section
);
$entity
->
set
(
'type'
,
$this
->
templateType
);
$create_or_update
=
$form_state
->
getValue
(
'create_or_update'
);
// Build a new template from the form values.
if
(
$create_or_update
===
'create'
)
{
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity
=
parent
::
buildEntity
(
$form
,
$form_state
);
$entity
->
set
(
'type'
,
$this
->
templateType
);
}
// Load the selected template to update.
else
{
$template_id
=
$form_state
->
getValue
(
'existing_template'
);
$entity
=
$this
->
entityTypeManager
->
getStorage
(
'section_library_template'
)
->
load
(
$template_id
);
// @todo Update template image at least.
// If not hiding custom fields, need to approach this differently.
}
$entity
->
set
(
'layout_section'
,
$deep_cloned_section
);
$entity
->
set
(
'entity_type'
,
$layout_entity
->
getEntityTypeId
());
$entity
->
set
(
'entity_id'
,
$layout_entity
->
id
());
...
...
@@ -241,7 +309,7 @@ class AddSectionToLibraryForm extends ContentEntityForm {
*/
public
function
titleCallback
(
string
$template_type
=
'section'
):
string
{
$label
=
$this
->
config
->
get
(
$template_type
.
'_label'
);
return
$this
->
t
(
'Add %type
to
Library'
,
[
'%type'
=>
$label
]);
return
$this
->
t
(
'Add
or Update
%type
in
Library'
,
[
'%type'
=>
$label
]);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment