class IndexForm in Elasticsearch Connector 8
Same name and namespace in other branches
- 8.7 src/Form/IndexForm.php \Drupal\elasticsearch_connector\Form\IndexForm
- 8.2 src/Form/IndexForm.php \Drupal\elasticsearch_connector\Form\IndexForm
- 8.5 src/Form/IndexForm.php \Drupal\elasticsearch_connector\Form\IndexForm
- 8.6 src/Form/IndexForm.php \Drupal\elasticsearch_connector\Form\IndexForm
Form controller for node type forms.
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\elasticsearch_connector\Form\IndexForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of IndexForm
File
- src/
Form/ IndexForm.php, line 20 - Contains \Drupal\elasticsearch_connector\Form\IndexForm.
Namespace
Drupal\elasticsearch_connector\FormView source
class IndexForm extends EntityForm {
/**
* The entity manager.
*
* This object members must be set to anything other than private in order for
* \Drupal\Core\DependencyInjection\DependencySerialization to be detected.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* Constructs an IndexForm object.
*
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
*/
public function __construct(EntityManager $entity_manager) {
// Setup object members.
$this->entityManager = $entity_manager;
}
/**
* Get the entity manager.
*
* @return \Drupal\Core\Entity\EntityManager
* An instance of EntityManager.
*/
protected function getEntityManager() {
return $this->entityManager;
}
/**
* Get the cluster storage controller.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* An instance of EntityStorageInterface.
*/
protected function getClusterStorage() {
return $this
->getEntityManager()
->getStorage('elasticsearch_cluster');
}
/**
* Get the index storage controller.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* An instance of EntityStorageInterface.
*/
protected function getIndexStorage() {
return $this
->getEntityManager()
->getStorage('elasticsearch_index');
}
/**
* Get all clusters.
*
* @return array
* All clusters
*/
protected function getAllClusters() {
$options = array();
foreach ($this
->getClusterStorage()
->loadMultiple() as $cluster_machine_name) {
$options[$cluster_machine_name->cluster_id] = $cluster_machine_name;
}
return $options;
}
/**
* Get cluster field.
*
* @param string $field
* Field name.
*
* @return array
* All clusters' fields.
*/
protected function getClusterField($field) {
$clusters = $this
->getAllClusters();
$options = array();
foreach ($clusters as $cluster) {
$options[$cluster->{$field}] = $cluster->{$field};
}
return $options;
}
/**
* Return url of the selected cluster.
*
* @param string $id
* Cluster id.
*
* @return string
* Cluster url.
*/
protected function getSelectedClusterUrl($id) {
$result = NULL;
$clusters = $this
->getAllClusters();
foreach ($clusters as $cluster) {
if ($cluster->cluster_id == $id) {
$result = $cluster->url;
}
}
return $result;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
if ($form_state
->isRebuilding()) {
$this->entity = $this
->buildEntity($form, $form_state);
}
$form = parent::form($form, $form_state);
$index = $this
->getEntity();
$form['#title'] = $this
->t('Index');
$this
->buildEntityForm($form, $form_state, $index);
return $form;
}
/**
* {@inheritdoc}
*/
public function buildEntityForm(array &$form, FormStateInterface $form_state, Index $index) {
$form['index'] = array(
'#type' => 'value',
'#value' => $index,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Index name'),
'#required' => TRUE,
'#default_value' => '',
'#description' => t('Enter the index name.'),
);
$form['index_id'] = array(
'#type' => 'machine_name',
'#title' => t('Index id'),
'#default_value' => '',
'#maxlength' => 125,
'#description' => t('Unique, machine-readable identifier for this Index'),
'#machine_name' => array(
'exists' => array(
$this
->getIndexStorage(),
'load',
),
'source' => array(
'name',
),
'replace_pattern' => '[^a-z0-9_]+',
'replace' => '_',
),
'#required' => TRUE,
'#disabled' => !empty($index->index_id),
);
// Here server refers to the elasticsearch cluster.
$form['server'] = array(
'#type' => 'radios',
'#title' => $this
->t('Server'),
'#default_value' => !empty($index->server) ? $index->server : Cluster::getDefaultCluster(),
'#description' => $this
->t('Select the server this index should reside on. Index can not be enabled without connection to valid server.'),
'#options' => $this
->getClusterField('cluster_id'),
'#weight' => 9,
'#required' => TRUE,
);
$form['num_of_shards'] = array(
'#type' => 'textfield',
'#title' => t('Number of shards'),
'#required' => TRUE,
'#default_value' => 5,
'#description' => t('Enter the number of shards for the index.'),
);
$form['num_of_replica'] = array(
'#type' => 'textfield',
'#title' => t('Number of replica'),
'#default_value' => 1,
'#description' => t('Enter the number of shards replicas.'),
);
}
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
$values = $form_state
->getValues();
if (!preg_match('/^[a-z][a-z0-9_]*$/i', $values['name'])) {
$form_state
->setErrorByName('name', t('Enter an index name that begins with a letter and contains only letters, numbers, and underscores.'));
}
if (!is_numeric($values['num_of_shards']) || $values['num_of_shards'] < 1) {
$form_state
->setErrorByName('num_of_shards', t('Invalid number of shards.'));
}
if (!is_numeric($values['num_of_replica'])) {
$form_state
->setErrorByName('num_of_replica', t('Invalid number of replica.'));
}
}
/**
* {@inheritdoc}
*/
public function submit(array $form, FormStateInterface $form_state) {
$values = $values;
$cluster_url = self::getSelectedClusterUrl($values['server']);
$client = Cluster::getClientByUrls(array(
$cluster_url,
));
$index_params = array();
if ($client) {
try {
$index_params['index'] = $values['index_id'];
$index_params['body']['settings']['number_of_shards'] = $values['num_of_shards'];
$index_params['body']['settings']['number_of_replicas'] = $values['num_of_replica'];
$index_params['body']['settings']['cluster_machine_name'] = $values['server'];
} catch (\Exception $e) {
drupal_set_message($e
->getMessage(), 'error');
}
try {
$response = $client
->indices()
->create($index_params);
if (Cluster::elasticsearchCheckResponseAck($response)) {
drupal_set_message(t('The index %index having id %index_id has been successfully created.', array(
'%index' => $values['name'],
'%index_id' => $values['index_id'],
)));
}
else {
drupal_set_message(t('Fail to create the index %index having id @index_id', array(
'%index' => $values['name'],
'@index_id' => $values['index_id'],
)), 'error');
}
} catch (\Exception $e) {
drupal_set_message($e
->getMessage(), 'error');
}
}
return parent::submit($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$index = $this->entity;
$index
->save();
drupal_set_message(t('Index %label has been added.', array(
'%label' => $index
->label(),
)));
$form_state
->setRedirect('elasticsearch_connector.clusters');
}
}
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:: |
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 |
IndexForm:: |
protected | property | The entity manager. | |
IndexForm:: |
public | function | ||
IndexForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
IndexForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
IndexForm:: |
protected | function | Get all clusters. | |
IndexForm:: |
protected | function | Get cluster field. | |
IndexForm:: |
protected | function | Get the cluster storage controller. | |
IndexForm:: |
protected | function | Get the entity manager. | |
IndexForm:: |
protected | function | Get the index storage controller. | |
IndexForm:: |
protected | function | Return url of the selected cluster. | |
IndexForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
IndexForm:: |
public | function | ||
IndexForm:: |
public | function | ||
IndexForm:: |
public | function | Constructs an IndexForm object. | |
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. | |
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. |