Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
preview_link
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
preview_link
Merge requests
!18
Resolve
#3156692
"Provide settings form"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve
#3156692
"Provide settings form"
issue/preview_link-3156692:3156692-provide-settings-form
into
2.1.x
Overview
12
Commits
12
Pipelines
9
Changes
6
All threads resolved!
Show all comments
Merged
nterbogt
requested to merge
issue/preview_link-3156692:3156692-provide-settings-form
into
2.1.x
1 year ago
Overview
12
Commits
12
Pipelines
9
Changes
6
All threads resolved!
Show all comments
Expand
Closes
#3156692
0
0
Merge request reports
Compare
2.1.x
version 9
469e4fe1
1 year ago
version 8
527a2338
1 year ago
version 7
4dd782fd
1 year ago
version 6
70e47a73
1 year ago
version 5
a9db16a1
1 year ago
version 4
56f52033
1 year ago
version 3
6d143550
1 year ago
version 2
32355677
1 year ago
version 1
94f7dce2
1 year ago
2.1.x (base)
and
latest version
latest version
469e4fe1
12 commits,
1 year ago
version 9
469e4fe1
12 commits,
1 year ago
version 8
527a2338
11 commits,
1 year ago
version 7
4dd782fd
10 commits,
1 year ago
version 6
70e47a73
9 commits,
1 year ago
version 5
a9db16a1
8 commits,
1 year ago
version 4
56f52033
7 commits,
1 year ago
version 3
6d143550
6 commits,
1 year ago
version 2
32355677
5 commits,
1 year ago
version 1
94f7dce2
2 commits,
1 year ago
6 files
+
320
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
6
Search (e.g. *.vue) (Ctrl+P)
src/Form/PreviewLinkSettingsForm.php
0 → 100644
+
203
−
0
Options
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\preview_link\Form
;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Entity\EntityTypeBundleInfoInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Form\ConfigFormBase
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\preview_link
\PreviewLinkUtility
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Allow settings to be changed from the UI for preview link.
*/
class
PreviewLinkSettingsForm
extends
ConfigFormBase
{
/**
* Constructs a PreviewLinkSettingsForm.
*/
public
function
__construct
(
ConfigFactoryInterface
$configFactory
,
protected
EntityTypeBundleInfoInterface
$bundleInfo
,
protected
EntityTypeManagerInterface
$entityTypeManager
,
)
{
parent
::
__construct
(
$configFactory
);
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
)
{
return
new
static
(
$container
->
get
(
'config.factory'
),
$container
->
get
(
'entity_type.bundle.info'
),
$container
->
get
(
'entity_type.manager'
),
);
}
/**
* {@inheritdoc}
*/
protected
function
getEditableConfigNames
():
array
{
return
[
$this
->
getConfigName
(),
];
}
/**
* A method to get the config name.
*
* @return string
* The config name.
*/
private
function
getConfigName
():
string
{
return
'preview_link.settings'
;
}
/**
* {@inheritdoc}
*/
public
function
getFormId
():
string
{
return
'preview_link_settings_form'
;
}
/**
* {@inheritdoc}
*/
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
):
array
{
$config
=
$this
->
config
(
$this
->
getConfigName
());
$form
=
parent
::
buildForm
(
$form
,
$form_state
);
$form
[
'display_message'
]
=
[
'#type'
=>
'select'
,
'#title'
=>
$this
->
t
(
'Display message'
),
'#description'
=>
$this
->
t
(
"When to display a message to a user that the preview link was created."
),
'#options'
=>
[
'always'
=>
$this
->
t
(
'Always'
),
'subsequent'
=>
$this
->
t
(
'Subsequent'
),
'never'
=>
$this
->
t
(
'Never'
),
],
'#default_value'
=>
$config
->
get
(
'display_message'
)
?:
'subsequent'
,
];
$form
[
'bundles'
]
=
[
'#type'
=>
'details'
,
];
$form
[
'bundles'
][
'help'
]
=
[
'#markup'
=>
$this
->
t
(
'Enable entity type/bundles for use with preview link.'
),
];
$selectedOptions
=
$this
->
getSelectedEntityTypeOptions
();
$form
[
'bundles'
][
'enabled_entity_types'
]
=
[
'#type'
=>
'tableselect'
,
'#header'
=>
[
$this
->
t
(
'Entity type'
),
$this
->
t
(
'Bundle'
),
],
'#options'
=>
$this
->
getEntityTypeOptions
(),
'#default_value'
=>
array_fill_keys
(
$selectedOptions
,
TRUE
),
];
// Collapse the details element if anything is enabled.
$form
[
'bundles'
][
'#title'
]
=
$this
->
t
(
'Enabled types (@count)'
,
[
'@count'
=>
count
(
$selectedOptions
),
]);
$form
[
'bundles'
][
'#open'
]
=
count
(
$selectedOptions
)
===
0
;
$form
[
'multiple_entities'
]
=
[
'#type'
=>
'checkbox'
,
'#title'
=>
'Multiple entities'
,
'#description'
=>
$this
->
t
(
'Whether preview links can reference multiple entities.'
),
'#default_value'
=>
$config
->
get
(
'multiple_entities'
),
];
$form
[
'expiry_seconds'
]
=
[
'#type'
=>
'number'
,
'#title'
=>
'Expiry seconds'
,
'#description'
=>
$this
->
t
(
'The number of seconds before a preview link expires.'
),
'#default_value'
=>
$config
->
get
(
'expiry_seconds'
)
?:
604800
,
'#min'
=>
1
,
];
return
$form
;
}
/**
* {@inheritdoc}
*/
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
$config
=
$this
->
config
(
$this
->
getConfigName
());
$config
->
set
(
'display_message'
,
$form_state
->
getValue
(
'display_message'
));
$config
->
set
(
'multiple_entities'
,
$form_state
->
getValue
(
'multiple_entities'
));
$config
->
set
(
'expiry_seconds'
,
$form_state
->
getValue
(
'expiry_seconds'
));
$config
->
clear
(
'enabled_entity_types'
);
foreach
(
array_keys
(
array_filter
(
$form_state
->
getValue
(
'enabled_entity_types'
)))
as
$enabledBundle
)
{
if
(
strpos
(
$enabledBundle
,
':'
)
!==
FALSE
)
{
[
$entityTypeId
,
$bundle
]
=
explode
(
':'
,
$enabledBundle
);
$bundles
=
$config
->
get
(
'enabled_entity_types.'
.
$entityTypeId
)
?:
[];
$bundles
[]
=
$bundle
;
$config
->
set
(
'enabled_entity_types.'
.
$entityTypeId
,
$bundles
);
}
else
{
$entityTypeId
=
$enabledBundle
;
$config
->
set
(
'enabled_entity_types.'
.
$entityTypeId
,
[]);
}
}
$config
->
save
();
parent
::
submitForm
(
$form
,
$form_state
);
}
/**
* The options available for the user to select for bundle types.
*
* @return array
* A 'entity_id:bundle' style array of possible options.
*/
protected
function
getEntityTypeOptions
():
array
{
$options
=
[];
$entityTypes
=
$this
->
entityTypeManager
->
getDefinitions
();
$entityTypes
=
array_filter
(
$entityTypes
,
[
PreviewLinkUtility
::
class
,
'isEntityTypeSupported'
,
]);
foreach
(
$entityTypes
as
$entityType
=>
$info
)
{
$options
[
$entityType
]
=
[
$info
?
[
'data'
=>
[
'#markup'
=>
'<strong>'
.
$info
->
getLabel
()
.
'</strong>'
]]
:
''
,
[
'data'
=>
[
'#markup'
=>
'<em>'
.
$this
->
t
(
'If selected and no bundles are selected, all bundles will be enabled.'
)
.
'</em>'
]],
];
foreach
(
$this
->
bundleInfo
->
getBundleInfo
(
$entityType
)
as
$bundle
=>
$bundleInfo
)
{
if
(
$entityType
===
$bundle
)
{
continue
;
}
$options
[
sprintf
(
'%s:%s'
,
$entityType
,
$bundle
)]
=
[
$info
?->
getLabel
()
?:
''
,
$bundleInfo
[
'label'
]
?:
''
,
];
}
}
return
$options
;
}
/**
* The enabled entities and bundles for preview link to apply to.
*
* @return array
* A 'entity_id:bundle' style array of selected options.
*/
protected
function
getSelectedEntityTypeOptions
():
array
{
$config
=
$this
->
config
(
$this
->
getConfigName
());
$configured
=
$config
->
get
(
'enabled_entity_types'
)
?:
[];
$selected
=
[];
foreach
(
$configured
as
$entityTypeId
=>
$bundles
)
{
$selected
[]
=
$entityTypeId
;
foreach
(
$bundles
as
$bundle
)
{
$selected
[]
=
sprintf
(
'%s:%s'
,
$entityTypeId
,
$bundle
);
}
}
return
$selected
;
}
}
Loading