protected function ScheduledUpdateTypeBaseForm::createNewFieldsElements in Scheduled Updates 8
Create options for create a new entity reference field.
Parameters
array $form:
\Drupal\Core\Form\FormStateInterface $form_state:
Return value
array
1 call to ScheduledUpdateTypeBaseForm::createNewFieldsElements()
- ScheduledUpdateTypeBaseForm::form in src/
Form/ ScheduledUpdateTypeBaseForm.php - Gets the actual form array to be built.
File
- src/
Form/ ScheduledUpdateTypeBaseForm.php, line 229 - Contains \Drupal\scheduled_updates\Form\ScheduledUpdateTypeBaseForm.
Class
Namespace
Drupal\scheduled_updates\FormCode
protected function createNewFieldsElements(array &$form, FormStateInterface $form_state) {
$entity_type = $this
->getCurrentEntityType($form_state);
$elements = [];
if ($entity_type && $this
->runnerSupportsEmbedded($this->entity
->getUpdateRunnerSettings())) {
$elements = [
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => 'Update Reference Options',
'#prefix' => '<div id="create-reference-fieldset">',
'#suffix' => '</div>',
];
$elements['bundles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Place field on bundles'),
'#description' => $this
->t('Choose which bundles to place this new field on.'),
'#options' => $this->updateUtils
->bundleOptions($entity_type),
'#required' => TRUE,
];
if ($target_bundle = $form_state
->get('target_bundle')) {
$elements['bundles']['#default_value'] = [
$target_bundle,
];
}
if ($this->moduleHandler
->moduleExists('inline_entity_form')) {
// Only works with Inline Entity Form for now
$existing_fields = $this
->existingReferenceFields();
if ($existing_fields) {
$elements['reference_field_options'] = [
'#type' => 'select',
'#title' => $this
->t('Reference Field Options'),
'#options' => [
'new' => $this
->t('Create new reference fields to enter updates.'),
'reuse' => $this
->t('Re-use an existing reference field to enter updates.'),
],
'#required' => TRUE,
];
}
else {
$elements['reference_field_options'] = [
'#type' => 'value',
'#value' => 'new',
];
}
$new_field_visible['#states'] = array(
'visible' => array(
':input[name="reference_settings[reference_field_options]"]' => array(
'value' => 'new',
),
),
'required' => array(
':input[name="reference_settings[reference_field_options]"]' => array(
'value' => 'new',
),
),
);
// Option #1 Create a New Field
$elements['new_field'] = [
'#type' => 'container',
'#title' => $this
->t('New Field'),
] + $new_field_visible;
$elements['new_field']['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#size' => 15,
] + $new_field_visible;
$field_prefix = $this
->config('field_ui.settings')
->get('field_prefix');
$elements['new_field']['field_name'] = [
'#type' => 'machine_name',
// This field should stay LTR even for RTL languages.
'#field_prefix' => '<span dir="ltr">' . $field_prefix,
'#field_suffix' => '</span>‎',
'#size' => 15,
'#description' => $this
->t('A unique machine-readable name containing letters, numbers, and underscores.'),
// Calculate characters depending on the length of the field prefix
// setting. Maximum length is 32.
'#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen($field_prefix),
'#machine_name' => array(
'source' => [
'type_dependent_elements',
'reference_settings',
'new_field',
'label',
],
'exists' => array(
$this,
'fieldNameExists',
),
),
'#required' => FALSE,
] + $new_field_visible;
$elements['new_field']['cardinality_container'] = array(
// Reset #parents so the additional container does not appear.
'#type' => 'fieldset',
'#title' => $this
->t('Update Limit'),
'#description' => $this
->t('How many updates updates should able to be added at one time?'),
'#attributes' => array(
'class' => array(
'container-inline',
'fieldgroup',
'form-composite',
),
),
) + $new_field_visible;
$elements['new_field']['cardinality_container']['cardinality'] = array(
'#type' => 'select',
'#title' => $this
->t('Allowed number of values'),
'#title_display' => 'invisible',
'#options' => array(
'number' => $this
->t('Limited'),
FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED => $this
->t('Unlimited'),
),
'#default_value' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
);
$elements['new_field']['cardinality_container']['cardinality_number'] = array(
'#type' => 'number',
'#default_value' => 1,
'#min' => 1,
'#title' => $this
->t('Limit'),
'#title_display' => 'invisible',
'#size' => 2,
'#states' => array(
'visible' => array(
':input[name="reference_settings[new_field][cardinality_container][cardinality]"]' => array(
'value' => 'number',
),
),
'disabled' => array(
':input[name="reference_settings[new_field][cardinality_container][cardinality]"]' => array(
'value' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
),
),
),
);
if ($existing_fields) {
$existing_field_visible['#states'] = array(
'visible' => array(
':input[name="reference_settings[reference_field_options]"]' => array(
'value' => 'reuse',
),
),
'required' => array(
':input[name="reference_settings[reference_field_options]"]' => array(
'value' => 'reuse',
),
),
);
$existing_options = [];
foreach ($existing_fields as $existing_field) {
// @todo Get bundle labels.
$bundles = array_keys($existing_field['bundles']);
$field_info = array_shift($existing_field['bundles']);
$existing_options[$field_info['field_id']] = $field_info['label'] . ': ' . $this
->t('Used on') . ' ' . implode(', ', $bundles);
}
$elements['existing_field'] = [
'#type' => 'radios',
'#title' => $this
->t('Existing Fields'),
'#options' => $existing_options,
] + $existing_field_visible;
}
}
else {
$markup = '<p>' . $this
->t('It is recommended that you use the <a href="https://www.drupal.org/project/inline_entity_form" >Inline Entity Form</a> module when creating updates directly on the entities to be updated.') . '</p>';
$markup .= '<p>' . $this
->t('Only proceed if you have an alternative method of creating new update entities on entities to be updated.');
$elements['notice'] = [
'#type' => 'markup',
'#markup' => $markup,
];
}
}
return $elements;
}