class RequiredContext

Same name and namespace in other branches
  1. 8.x-3.x src/Form/RequiredContext.php \Drupal\ctools\Form\RequiredContext

Required Context Form.

Hierarchy

Expanded class hierarchy of RequiredContext

File

src/Form/RequiredContext.php, line 17

Namespace

Drupal\ctools\Form
View source
abstract class RequiredContext extends FormBase {
    
    /**
     * @var \Drupal\Core\TypedData\TypedDataManager
     */
    protected $typedDataManager;
    
    /**
     * The builder of form.
     *
     * @var \Drupal\Core\Form\FormBuilderInterface
     */
    protected $formBuilder;
    
    /**
     * @var string
     */
    protected $machine_name;
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('typed_data_manager'), $container->get('form_builder'));
    }
    
    /**
     * Required Context Form constructor.
     *
     * @param \Drupal\Component\Plugin\PluginManagerInterface $typed_data_manager
     *   The Typed Data Manager.
     * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
     *   The Form Builder.
     */
    public function __construct(PluginManagerInterface $typed_data_manager, FormBuilderInterface $form_builder) {
        $this->typedDataManager = $typed_data_manager;
        $this->formBuilder = $form_builder;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'ctools_required_context_form';
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {
        $cached_values = $form_state->getTemporaryValue('wizard');
        $this->machine_name = $cached_values['id'];
        $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
        $options = [];
        foreach ($this->typedDataManager
            ->getDefinitions() as $plugin_id => $definition) {
            $options[$plugin_id] = (string) $definition['label'];
        }
        $form['items'] = [
            '#type' => 'markup',
            '#prefix' => '<div id="configured-contexts">',
            '#suffix' => '</div>',
            '#theme' => 'table',
            '#header' => [
                $this->t('Information'),
                $this->t('Description'),
                $this->t('Operations'),
            ],
            '#rows' => $this->renderContexts($cached_values),
            '#empty' => $this->t('No required contexts have been configured.'),
        ];
        $form['contexts'] = [
            '#type' => 'select',
            '#options' => $options,
        ];
        $form['add'] = [
            '#type' => 'submit',
            '#name' => 'add',
            '#value' => $this->t('Add required context'),
            '#ajax' => [
                'callback' => [
                    $this,
                    'add',
                ],
                'event' => 'click',
            ],
            '#submit' => [
                'callback' => [
                    $this,
                    'submitform',
                ],
            ],
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        $cached_values = $form_state->getTemporaryValue('wizard');
        [
            $route_name,
            $route_parameters,
        ] = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('contexts'));
        $form_state->setRedirect($route_name . '.edit', $route_parameters);
    }
    
    /**
     * Custom ajax form submission handler.
     *
     * @param array $form
     * @param \Drupal\Core\Form\FormStateInterface $form_state
     *
     * @return \Drupal\Core\Ajax\AjaxResponse
     */
    public function add(array &$form, FormStateInterface $form_state) {
        $context = $form_state->getValue('contexts');
        $content = $this->formBuilder
            ->getForm($this->getContextClass(), $context, $this->getTempstoreId(), $this->machine_name);
        $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
        $response = new AjaxResponse();
        $response->addCommand(new OpenModalDialogCommand($this->t('Configure Required Context'), $content, [
            'width' => '700',
        ]));
        return $response;
    }
    
    /**
     * Render The contexts in the form.
     *
     * @param $cached_values
     *   Cached context values.
     *
     * @return array
     *   The rendered contexts.
     */
    public function renderContexts($cached_values) {
        $configured_contexts = [];
        foreach ($this->getContexts($cached_values) as $row => $context) {
            [
                $plugin_id,
                $label,
                $machine_name,
                $description,
            ] = array_values($context);
            [
                $route_name,
                $route_parameters,
            ] = $this->getOperationsRouteInfo($cached_values, $cached_values['id'], $row);
            $build = [
                '#type' => 'operations',
                '#links' => $this->getOperations($route_name, $route_parameters),
            ];
            $configured_contexts[] = [
                $this->t('<strong>Label:</strong> @label<br /> <strong>Type:</strong> @type', [
                    '@label' => $label,
                    '@type' => $plugin_id,
                ]),
                $this->t('@description', [
                    '@description' => $description,
                ]),
                'operations' => [
                    'data' => $build,
                ],
            ];
        }
        return $configured_contexts;
    }
    
    /**
     * Retrieve Form Operations
     *
     * @param $route_name_base
     *   The base route name.
     * @param array $route_parameters
     *   Route Parameters.
     *
     * @return array
     *   The available operations.
     */
    protected function getOperations($route_name_base, array $route_parameters = []) {
        $operations['edit'] = [
            'title' => $this->t('Edit'),
            'url' => new Url($route_name_base . '.edit', $route_parameters),
            'weight' => 10,
            'attributes' => [
                'class' => [
                    'use-ajax',
                ],
                'data-accepts' => 'application/vnd.drupal-modal',
                'data-dialog-options' => json_encode([
                    'width' => 700,
                ]),
            ],
            'ajax' => [
                '',
            ],
        ];
        $route_parameters['id'] = $route_parameters['context'];
        $operations['delete'] = [
            'title' => $this->t('Delete'),
            'url' => new Url($route_name_base . '.delete', $route_parameters),
            'weight' => 100,
            'attributes' => [
                'class' => [
                    'use-ajax',
                ],
                'data-accepts' => 'application/vnd.drupal-modal',
                'data-dialog-options' => json_encode([
                    'width' => 700,
                ]),
            ],
        ];
        return $operations;
    }
    
    /**
     * Return a subclass of '\Drupal\ctools\Form\ContextConfigure'.
     *
     * The ContextConfigure class is designed to be subclassed with custom route
     * information to control the modal/redirect needs of your use case.
     *
     * @return string
     *   The Context Class.
     */
    protected abstract function getContextClass();
    
    /**
     * Provide the tempstore id for your specified use case.
     *
     * @return string
     *   The Tempstore ID.
     */
    protected abstract function getTempstoreId();
    
    /**
     * Document the route name and parameters for edit/delete context operations.
     *
     * The route name returned from this method is used as a "base" to which
     * ".edit" and ".delete" are appended in the getOperations() method.
     * Subclassing '\Drupal\ctools\Form\ContextConfigure' and
     * '\Drupal\ctools\Form\RequiredContextDelete' should set you up for using
     * this approach quite seamlessly.
     *
     * @param mixed $cached_values
     *  The Cached Values.
     * @param string $machine_name
     *  The form machine name.
     * @param string $row
     *  The form row to operate on.
     *
     * @return array
     *   In the format of
     *   return ['route.base.name',
     *     ['machine_name' => $machine_name, 'context' => $row]];
     */
    protected abstract function getOperationsRouteInfo(mixed $cached_values, string $machine_name, string $row);
    
    /**
     * Custom logic for retrieving the contexts array from cached_values.
     *
     * @param array $cached_values
     *   The Cached Values.
     *
     * @return array
     *   The Contexts.
     */
    protected abstract function getContexts(array $cached_values);

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user. 2
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 57
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 17
MessengerTrait::messenger public function Gets the messenger. 17
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 2
RedirectDestinationTrait::getDestinationArray protected function Prepares a &#039;destination&#039; URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
RequiredContext::$formBuilder protected property The builder of form.
RequiredContext::$machine_name protected property
RequiredContext::$typedDataManager protected property
RequiredContext::add public function Custom ajax form submission handler.
RequiredContext::buildForm public function Form constructor. Overrides FormInterface::buildForm
RequiredContext::create public static function Instantiates a new instance of this class. Overrides FormBase::create
RequiredContext::getContextClass abstract protected function Return a subclass of &#039;\Drupal\ctools\Form\ContextConfigure&#039;.
RequiredContext::getContexts abstract protected function Custom logic for retrieving the contexts array from cached_values.
RequiredContext::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
RequiredContext::getOperations protected function Retrieve Form Operations
RequiredContext::getOperationsRouteInfo abstract protected function Document the route name and parameters for edit/delete context operations.
RequiredContext::getTempstoreId abstract protected function Provide the tempstore id for your specified use case.
RequiredContext::renderContexts public function Render The contexts in the form.
RequiredContext::submitForm public function Form submission handler. Overrides FormInterface::submitForm
RequiredContext::__construct public function Required Context Form constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.