Same name and namespace in other branches
  1. 8.x-1.x src/EntityClone/Config/ConfigEntityCloneFormBase.php \Drupal\entity_clone\EntityClone\Config\ConfigEntityCloneFormBase 1 comment

Class Config Entity Clone FormBase.

Hierarchy

Expanded class hierarchy of ConfigEntityCloneFormBase

1 file declares its use of ConfigEntityCloneFormBase
entity_clone.module in ./entity_clone.module
Contains entity_clone.module.

File

src/EntityClone/Config/ConfigEntityCloneFormBase.php, line 17

Namespace

Drupal\entity_clone\EntityClone\Config
View source
class ConfigEntityCloneFormBase implements EntityHandlerInterface, EntityCloneFormInterface {
    
    /**
     * The string translation.
     *
     * @var \Drupal\Core\StringTranslation\TranslationManager
     */
    protected $translationManager;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Constructs a new ConfigEntityCloneFormBase.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\StringTranslation\TranslationManager $translation_manager
     *   The string translation manager.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationManager $translation_manager) {
        $this->entityTypeManager = $entity_type_manager;
        $this->translationManager = $translation_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
        return new static($container->get('entity_type.manager'), $container->get('string_translation'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function formElement(EntityInterface $entity, $parent = TRUE) {
        $form = [];
        if ($this->entityTypeManager
            ->getDefinition($entity->getEntityTypeId())
            ->getKey('label')) {
            $form['label'] = [
                '#type' => 'textfield',
                '#title' => $this->translationManager
                    ->translate('New Label'),
                '#maxlength' => 255,
                '#required' => TRUE,
            ];
        }
        // In common casse, config entities IDs are limited to 64 characters ...
        $max_length = 64;
        if ($entity->getEntityType()
            ->getBundleOf()) {
            // ... Except for bundle definition, that are limited to 32 characters.
            $max_length = EntityTypeInterface::BUNDLE_MAX_LENGTH;
        }
        $form['id'] = [
            '#type' => 'machine_name',
            '#title' => $this->translationManager
                ->translate('New Id'),
            '#maxlength' => $max_length,
            '#required' => TRUE,
        ];
        // If entity must have a prefix
        // (e.g. entity_form_mode, entity_view_mode, ...).
        if (method_exists($entity, 'getTargetType')) {
            $form['id']['#field_prefix'] = $entity->getTargetType() . '.';
        }
        if (method_exists($entity, 'load')) {
            $form['id']['#machine_name'] = [
                'exists' => get_class($entity) . '::load',
            ];
        }
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getValues(FormStateInterface $form_state) {
        // If entity must have a prefix
        // (e.g. entity_form_mode, entity_view_mode, ...).
        $field_prefix = '';
        if (isset($form_state->getCompleteForm()['id']['#field_prefix'])) {
            $field_prefix = $form_state->getCompleteForm()['id']['#field_prefix'];
        }
        return [
            'id' => $field_prefix . $form_state->getValue('id'),
            'label' => $form_state->getValue('label'),
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConfigEntityCloneFormBase::$entityTypeManager protected property The entity type manager.
ConfigEntityCloneFormBase::$translationManager protected property The string translation.
ConfigEntityCloneFormBase::createInstance public static function
ConfigEntityCloneFormBase::formElement public function Get all specific form element. Overrides EntityCloneFormInterface::formElement 1
ConfigEntityCloneFormBase::getValues public function Get all new values provided by the specific form element. Overrides EntityCloneFormInterface::getValues
ConfigEntityCloneFormBase::__construct public function Constructs a new ConfigEntityCloneFormBase.