Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
serial
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
serial
Merge requests
!11
Draft: Series per Entity per Entity Reference
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Draft: Series per Entity per Entity Reference
issue/serial-1478012:1478012-series-per-entity
into
2.0.x
Overview
0
Commits
8
Pipelines
1
Changes
4
Open
Benjamin Melançon
requested to merge
issue/serial-1478012:1478012-series-per-entity
into
2.0.x
7 months ago
Overview
0
Commits
8
Pipelines
1
Changes
4
Expand
Issue
#1478012
0
0
Merge request reports
Compare
2.0.x
2.0.x (HEAD)
and
latest version
latest version
eb8e01ad
8 commits,
7 months ago
4 files
+
131
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
modules/serialper/src/Plugin/Field/FieldType/SerialPerEntityReferenceItem.php
0 → 100644
+
87
−
0
Options
<?php
namespace
Drupal\serialper\Plugin\Field\FieldType
;
use
Drupal\serial\Plugin\Field\FieldType\SerialItem
;
use
Drupal\Core\Field\FieldStorageDefinitionInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\TypedData\DataDefinition
;
use
Drupal\Core\TypedData\TranslatableInterface
;
/**
* Plugin implementation of the 'serialper' field type.
*
* @todo should not be translatable, by default
*
* @FieldType(
* id = "serialper",
* label = @Translation("Serial per Entity Reference"),
* description = @Translation("Auto increment per referenced entity serial field type."),
* category = "number",
* default_widget = "serial_default_widget",
* default_formatter = "serial_default_formatter"
* )
*/
class
SerialPerEntityReferenceItem
extends
SerialItem
{
/**
* {@inheritdoc}
*/
public
static
function
defaultStorageSettings
()
{
return
[
'reference_field'
=>
''
,
]
+
parent
::
defaultStorageSettings
();
}
/**
* {@inheritdoc}
*/
public
function
storageSettingsForm
(
array
&
$form
,
FormStateInterface
$form_state
,
$has_data
)
{
$element
=
[];
$options
=
[];
$fields
=
$this
->
getEntity
()
->
getFields
()
??
[];
foreach
(
$fields
as
$field_name
=>
$field_info
)
{
/** @var Drupal\Core\Field\EntityReferenceFieldItemList $field_info */
// Cannot use ->get('field_type') as it only works on non-base fields.
$field_definition
=
$field_info
->
getDataDefinition
();
$field_type
=
$field_definition
->
getType
()
??
''
;
if
(
$field_type
===
'entity_reference'
)
{
if
(
method_exists
(
$field_definition
,
'label'
))
{
// If there was no label, it's probably a whack entity reference field
// that we cannot use anyway, i don't understand why content type,
// revision_uid, uid, and menu_link base fields claim the entity
// reference mantel and then drop the ball on something so basic.
$options
[
$field_name
]
=
$field_definition
->
label
();
}
}
}
$element
[
'reference_field'
]
=
[
'#type'
=>
'select'
,
'#title'
=>
$this
->
t
(
'Reference field'
),
'#description'
=>
$this
->
t
(
'The field that contains the entity reference which will determine when this field starts a new series.'
),
'#default_value'
=>
$this
->
getSetting
(
'reference_field'
),
'#options'
=>
$options
,
'#required'
=>
TRUE
,
'#disabled'
=>
$has_data
,
];
return
$element
+
parent
::
storageSettingsForm
(
$form
,
$form_state
,
$has_data
);
}
/**
* {@inheritdoc}
*/
public
function
preSave
()
{
/** @var \Drupal\serial\SerialStorageInterface $serialStorage */
$serialStorage
=
\Drupal
::
getContainer
()
->
get
(
'serial.sql_storage'
);
$storageName
=
$serialStorage
->
createStorageNameFromField
(
$this
->
getFieldDefinition
(),
$this
->
getEntity
());
// Create table if it does not exist already.
if
(
!
\Drupal
::
database
()
->
schema
()
->
tableExists
(
$storageName
))
{
$serialStorage
->
createStorageFromName
(
$storageName
);
}
parent
::
preSave
();
}
}
Loading