Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
seeds_page
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
seeds_page
Commits
f1e36680
Commit
f1e36680
authored
9 months ago
by
Ahmad Alyasaki
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3443512
: Add Seeds Page Form settings
parent
0b5d7dfa
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
seeds_page.services.yml
+4
-0
4 additions, 0 deletions
seeds_page.services.yml
src/Form/SeedsPageForm.php
+188
-0
188 additions, 0 deletions
src/Form/SeedsPageForm.php
src/SeedsPageManager.php
+81
-0
81 additions, 0 deletions
src/SeedsPageManager.php
with
273 additions
and
0 deletions
seeds_page.services.yml
0 → 100644
+
4
−
0
View file @
f1e36680
services
:
seeds_page.manager
:
class
:
Drupal\seeds_page\SeedsPageManager
arguments
:
[
'
@renderer'
,
'
@image.factory'
,
'
@current_route_match'
,
'
@config.factory'
]
This diff is collapsed.
Click to expand it.
src/Form/SeedsPageForm.php
0 → 100644
+
188
−
0
View file @
f1e36680
<?php
namespace
Drupal\seeds_page\Form
;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Entity\ContentEntityType
;
use
Drupal\Core\Entity\EntityTypeBundleInfo
;
use
Drupal\Core\Entity\EntityTypeManager
;
use
Drupal\Core\Form\ConfigFormBase
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Routing\AccessAwareRouter
;
use
Drupal\seeds_page
\SeedsPageManager
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Class SeedsPageForm.
*/
class
SeedsPageForm
extends
ConfigFormBase
{
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected
$entityTypeManager
;
/**
* Router.
*
* @var \Drupal\Core\Routing\AccessAwareRouter
*/
protected
$router
;
/**
* Bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfo
*/
protected
$bundleInfo
;
/**
* Seeds Page Manager.
*
* @var \Drupal\seeds_page\SeedsPageManager
*/
protected
$seedsPageManager
;
/**
*
*/
public
function
__construct
(
ConfigFactoryInterface
$config_factory
,
EntityTypeManager
$entity_type_manager
,
AccessAwareRouter
$router
,
EntityTypeBundleInfo
$bundle_info
,
SeedsPageManager
$seeds_page_manager
)
{
parent
::
__construct
(
$config_factory
);
$this
->
entityTypeManager
=
$entity_type_manager
;
$this
->
router
=
$router
;
$this
->
bundleInfo
=
$bundle_info
;
$this
->
seedsPageManager
=
$seeds_page_manager
;
}
/**
*
*/
public
static
function
create
(
ContainerInterface
$container
)
{
return
new
static
(
$container
->
get
(
'config.factory'
),
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'router'
),
$container
->
get
(
'entity_type.bundle.info'
),
$container
->
get
(
'seeds_page.manager'
)
);
}
/**
* {@inheritdoc}
*/
public
function
getFormId
()
{
return
'seeds_page_form'
;
}
/**
*
*/
public
function
getEditableConfigNames
()
{
return
[
'seeds_page.config'
];
}
/**
* {@inheritdoc}
*/
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
)
{
$config
=
$this
->
config
(
'seeds_page.config'
);
$media
=
$this
->
entityTypeManager
->
getStorage
(
'media'
)
->
load
(
$config
->
get
(
'media_id'
));
$form
[
'media_wrapper'
]
=
[
'#type'
=>
'container'
,
];
$form
[
'field'
]
=
[
'#type'
=>
'select'
,
'#title'
=>
$this
->
t
(
"Field to Render"
),
'#options'
=>
$this
->
getMediaFields
(),
'#default_value'
=>
$config
->
get
(
'field'
),
];
$entity_types
=
$this
->
loadAllEntityTypes
();
foreach
(
$entity_types
as
$entity_type_id
=>
$entity_type
)
{
if
(
$this
->
seedsPageManager
->
isAcceptedEntity
(
$entity_type_id
))
{
$form
[
'entity_types_wrapper'
][
$entity_type_id
]
=
[
'#type'
=>
'checkboxes'
,
'#multiple'
=>
TRUE
,
'#title'
=>
$entity_type
[
'label'
],
'#options'
=>
$entity_type
[
'bundles'
],
'#default_value'
=>
$config
->
get
(
'entity_types'
)[
$entity_type_id
],
];
}
}
$form
[
'submit'
]
=
[
'#type'
=>
'submit'
,
'#value'
=>
$this
->
t
(
'Submit'
),
];
return
$form
;
}
/**
*
*/
private
function
loadAllEntityTypes
()
{
$entity_types
=
[];
$entity_definitions
=
\Drupal
::
entityTypeManager
()
->
getDefinitions
();
foreach
(
$entity_definitions
as
$entity_type_id
=>
$entity_definition
)
{
if
(
$entity_definition
instanceof
ContentEntityType
)
{
// Check if the entity has a landing page by checking its route.
$route_name
=
"entity.
$entity_type_id
.canonical"
;
$has_landing_route
=
$this
->
router
->
getRouteCollection
()
->
get
(
$route_name
);
if
(
$has_landing_route
)
{
$bundles
=
$this
->
bundleInfo
->
getBundleInfo
(
$entity_type_id
);
foreach
(
$bundles
as
$id
=>
$bundle
)
{
$entity_types
[
$entity_type_id
][
'bundles'
][
$id
]
=
$bundle
[
'label'
];
}
$entity_types
[
$entity_type_id
][
'label'
]
=
$entity_definition
->
getLabel
();
}
}
}
return
$entity_types
;
}
/**
*
*/
private
function
getMediaFields
()
{
$filtered_media_fields
=
[];
$media_fields
=
$this
->
entityTypeManager
->
getStorage
(
'field_storage_config'
)
->
loadByProperties
(
[
'settings'
=>
[
'target_type'
=>
'media'
,
],
'type'
=>
'entity_reference'
,
'deleted'
=>
FALSE
,
'status'
=>
1
,
]
);
foreach
(
$media_fields
as
$key
=>
$field
)
{
$field_name
=
explode
(
'.'
,
$key
)[
1
];
$filtered_media_fields
[
$field_name
]
=
$field_name
;
}
return
$filtered_media_fields
;
}
/**
* {@inheritdoc}
*/
public
function
validateForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
parent
::
validateForm
(
$form
,
$form_state
);
}
/**
* {@inheritdoc}
*/
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
$config
=
$this
->
configFactory
->
getEditable
(
'seeds_page.config'
);
$config
->
set
(
'entity_types'
,
$form_state
->
getValue
(
'entity_types_wrapper'
));
$config
->
set
(
'field'
,
$form_state
->
getValue
(
'field'
));
$config
->
save
();
parent
::
submitForm
(
$form
,
$form_state
);
}
}
This diff is collapsed.
Click to expand it.
src/SeedsPageManager.php
0 → 100644
+
81
−
0
View file @
f1e36680
<?php
namespace
Drupal\seeds_page
;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Image\ImageFactory
;
use
Drupal\Core\Render\Renderer
;
use
Drupal\Core\Routing\CurrentRouteMatch
;
/**
*
*/
class
SeedsPageManager
{
/**
* Accepted entity types supported by Seeds to render the banner.
*/
protected
const
ACCEPTED_ENTITY_TYPES
=
[
'node'
,
'media'
,
'taxonomy_term'
,
'user'
,
];
/**
* Renderer.
*
* @var \Drupal\Core\Render\Renderer
*/
protected
$renderer
;
/**
* Image Factory.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected
$imageFactory
;
/**
* Current Route Match.
*
* @var \Drupal\Core\Routing\CurrentRouteMatch
*/
protected
$routeMatch
;
/**
* Config Factory definition.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected
$configFactory
;
/**
* Construct new SeedsPageManager object.
*/
public
function
__construct
(
Renderer
$renderer
,
ImageFactory
$image_factory
,
CurrentRouteMatch
$route_match
,
ConfigFactoryInterface
$config_factory
)
{
$this
->
imageFactory
=
$image_factory
;
$this
->
renderer
=
$renderer
;
$this
->
routeMatch
=
$route_match
;
$this
->
configFactory
=
$config_factory
;
}
/**
* Checks if the passed entity type id is supported by Seeds.
*
* @param $entity_type_id
* The id of the entity type to be checked.
*
* @return bool
*/
public
function
isAcceptedEntity
(
$entity_type_id
)
{
return
in_array
(
$entity_type_id
,
self
::
ACCEPTED_ENTITY_TYPES
,
TRUE
);
}
}
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