class RelationTypeForm in Relation 8
Same name and namespace in other branches
- 8.2 src/RelationTypeForm.php \Drupal\relation\RelationTypeForm
Form controller for relation edit form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\relation\RelationTypeForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of RelationTypeForm
File
- src/
RelationTypeForm.php, line 17 - Definition of Drupal\relation\RelationTypeForm.
Namespace
Drupal\relationView source
class RelationTypeForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\relation\Entity\RelationType $relation_type */
$relation_type = $this->entity;
if ($this->operation == 'add') {
$form['#title'] = $this
->t('Add relation type');
}
elseif ($this->operation == 'edit') {
$form['#title'] = $this
->t('Edit %label relation type', array(
'%label' => $relation_type
->label(),
));
}
$form['#attached'] = array(
'library' => array(
'relation/drupal.relation',
),
);
$form['labels'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'relation-type-form-table',
),
),
'#suffix' => '<div class="clearfix"></div>',
);
$form['labels']['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#description' => t('Display name of the relation type. This is also used as the predicate in natural language formatters (ie. if A is related to B, you get "A [label] B")'),
'#default_value' => $relation_type
->label(),
'#size' => 40,
'#required' => TRUE,
);
$form['labels']['id'] = array(
'#type' => 'machine_name',
'#default_value' => $relation_type
->id(),
'#maxlength' => 32,
'#disabled' => !$relation_type
->isNew(),
'#machine_name' => array(
'source' => array(
'labels',
'label',
),
'exists' => '\\Drupal\\relation\\Entity\\RelationType::load',
),
);
$form['labels']['reverse_label'] = array(
'#type' => 'textfield',
'#size' => 40,
'#title' => t('Reverse label'),
'#description' => t('Reverse label of the relation type. This is used as the predicate by formatters of directional relations, when you need to display the reverse direction (ie. from the target entity to the source entity). If this is not supplied, the forward label is used.'),
'#default_value' => $relation_type
->reverseLabel(),
'#states' => array(
'visible' => array(
':input[name="directional"]' => array(
'checked' => TRUE,
),
':input[name="advanced[max_arity]"]' => array(
'!value' => '1',
),
),
'required' => array(
':input[name="directional"]' => array(
'checked' => TRUE,
),
':input[name="advanced[max_arity]"]' => array(
'!value' => '1',
),
),
),
);
$form['directional'] = array(
'#type' => 'checkbox',
'#title' => 'Directional',
'#description' => t('A directional relation is one that does not imply the same relation in the reverse direction. For example, a "likes" relation is directional (A likes B does not neccesarily mean B likes A), whereas a "similar to" relation is non-directional (A similar to B implies B similar to A. Non-directional relations are also known as symmetric relations.'),
'#default_value' => $relation_type->directional,
'#states' => array(
'invisible' => array(
':input[name="advanced[max_arity]"]' => array(
'value' => '1',
),
),
),
);
// More advanced options, hide by default.
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 50,
);
$form['advanced']['transitive'] = array(
'#type' => 'checkbox',
'#title' => t('Transitive'),
'#description' => t('A transitive relation implies that the relation passes through intermediate entities (ie. A=>B and B=>C implies that A=>C). For example "Ancestor" is transitive: your ancestor\'s ancestor is also your ancestor. But a "Parent" relation is non-transitive: your parent\'s parent is not your parent, but your grand-parent.'),
'#default_value' => $relation_type->transitive,
'#states' => array(
'invisible' => array(
':input[name="advanced[max_arity]"]' => array(
'value' => '1',
),
),
),
);
$form['advanced']['r_unique'] = array(
'#type' => 'checkbox',
'#title' => t('Unique'),
'#description' => t('Whether relations of this type are unique (ie. they can not contain exactly the same end points as other relations of this type).'),
'#default_value' => $relation_type->r_unique,
);
// These should probably be changed to numerical (validated) textfields.
$options = array(
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8',
);
$form['advanced']['min_arity'] = array(
'#type' => 'select',
'#title' => t('Minimum Arity'),
'#options' => $options,
'#description' => t('Minimum number of entities joined by relations of this type (e.g. three siblings in one relation). <em>In nearly all cases you will want to leave this set to 2</em>.'),
'#default_value' => $relation_type->min_arity ? $relation_type->min_arity : 2,
);
$options = array(
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8',
'0' => t('Infinite'),
);
$form['advanced']['max_arity'] = array(
'#type' => 'select',
'#title' => t('Maximum Arity'),
'#options' => $options,
'#description' => t('Maximum number of entities joined by relations of this type. <em>In nearly all cases you will want to leave this set to 2</em>.'),
'#default_value' => isset($relation_type->max_arity) ? $relation_type->max_arity : 2,
);
$options_bundles = array();
$entity_info = \Drupal::entityTypeManager()
->getDefinitions();
foreach (\Drupal::service('entity_type.bundle.info')
->getAllBundleInfo() as $entity_type => $bundles) {
$entity_label = $entity_info[$entity_type]
->getLabel();
$entity_label_string = (string) $entity_label;
$options_bundles[$entity_label_string]["{$entity_type}:*"] = 'all ' . $entity_label_string . ' bundles';
foreach ($bundles as $bundle_id => $bundle) {
$options_bundles[$entity_label_string]["{$entity_type}:{$bundle_id}"] = $bundle['label'];
}
}
$form['endpoints'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'relation-type-form-table',
),
),
'#suffix' => '<div class="clearfix"></div>',
);
$form['endpoints']['source_bundles'] = array(
'#type' => 'select',
'#title' => t('Source bundles'),
'#options' => $options_bundles,
'#size' => count($options_bundles, COUNT_RECURSIVE),
'#default_value' => $relation_type->source_bundles,
'#multiple' => TRUE,
'#required' => TRUE,
'#description' => t('Select which bundles may be endpoints on relations of this type. Selecting "all <em>entity</em> bundles" includes bundles created in the future.'),
);
$form['endpoints']['target_bundles'] = array(
'#type' => 'select',
'#title' => t('Target bundles'),
'#options' => $options_bundles,
'#size' => count($options_bundles, COUNT_RECURSIVE),
'#default_value' => $relation_type->target_bundles,
'#multiple' => TRUE,
'#description' => t('Select which bundles may be endpoints on relations of this type. Selecting "all <em>entity</em> bundles" includes bundles created in the future.'),
'#states' => array(
'visible' => array(
':input[name="directional"]' => array(
'checked' => TRUE,
),
':input[name="advanced[max_arity]"]' => array(
'!value' => '1',
),
),
'required' => array(
':input[name="directional"]' => array(
'checked' => TRUE,
),
':input[name="advanced[max_arity]"]' => array(
'!value' => '1',
),
),
),
);
return parent::form($form, $form_state, $relation_type);
}
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
$min_arity = $form_state
->getValue('min_arity');
$max_arity = $form_state
->getValue('max_arity');
// Empty max arity indicates infinite arity.
if ($max_arity && $min_arity > $max_arity) {
$form_state
->setErrorByName('min_arity', t('Minimum arity cannot be more than maximum arity.'));
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$relation_type = $this->entity;
$save_message = $relation_type
->isNew() ? t('The %relation_type relation type has been created.', array(
'%relation_type' => $relation_type
->id(),
)) : t('The %relation_type relation type has been saved.', array(
'%relation_type' => $relation_type
->id(),
));
if ($relation_type
->save()) {
drupal_set_message($save_message);
$form_state
->setRedirectUrl($relation_type
->urlInfo());
}
else {
drupal_set_message(t('Error saving relation type.', 'error'));
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
RelationTypeForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
RelationTypeForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
RelationTypeForm:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |