View source
<?php
namespace Drupal\rules\Form;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rules\Engine\ExpressionManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class RulesComponentFormBase extends EntityForm {
protected $expressionManager;
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.rules_expression'));
}
public function __construct(ExpressionManagerInterface $expression_manager) {
$this->expressionManager = $expression_manager;
}
public function form(array $form, FormStateInterface $form_state) {
$form['#entity_builders'][] = '::entityTagsBuilder';
$form['settings'] = [
'#type' => 'details',
'#title' => $this
->t('Settings'),
'#open' => $this->entity
->isNew(),
];
$form['settings']['label'] = [
'#type' => 'textfield',
'#description' => $this
->t('Enter a name to be used to identify your component in the administrative interface.'),
'#title' => $this
->t('Label'),
'#default_value' => $this->entity
->label(),
'#required' => TRUE,
];
$form['settings']['id'] = [
'#type' => 'machine_name',
'#description' => $this
->t('A unique machine-readable name for your component. Can only contain lowercase letters, numbers, and underscores.'),
'#disabled' => !$this->entity
->isNew(),
'#default_value' => $this->entity
->id(),
'#machine_name' => [
'exists' => [
$this,
'exists',
],
'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
'source' => [
'settings',
'label',
],
'error' => $this
->t('The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".'),
],
];
$form['settings']['keywords'] = [
'#type' => 'textfield',
'#title' => $this
->t('Tags'),
'#default_value' => implode(', ', $this->entity
->getTags()),
'#description' => $this
->t('Enter a list of comma-separated keywords here; e.g., "notification, publishing". Tags are keywords used for filtering available components in the administration interface.'),
'#required' => FALSE,
];
$form['settings']['description'] = [
'#type' => 'textarea',
'#default_value' => $this->entity
->getDescription(),
'#description' => $this
->t('Enter a description for this component, to help document what this component is intended to do.'),
'#title' => $this
->t('Description'),
];
return parent::form($form, $form_state);
}
public function entityTagsBuilder($entity_type, ConfigEntityInterface $entity, array $form, FormStateInterface $form_state) {
$tags = [];
$input_tags = $form_state
->getValue('keywords');
if (trim($input_tags) != '') {
$tags = array_map('trim', explode(',', $input_tags));
}
$entity
->set('tags', $tags);
}
public function exists($id) {
$type = $this->entity
->getEntityTypeId();
return (bool) $this->entityTypeManager
->getStorage($type)
->load($id);
}
}