class ManageResolverRelationships

Same name in other branches
  1. 4.0.x src/Form/ManageResolverRelationships.php \Drupal\ctools\Form\ManageResolverRelationships

Provider manage resolver relationships.

Hierarchy

Expanded class hierarchy of ManageResolverRelationships

File

src/Form/ManageResolverRelationships.php, line 18

Namespace

Drupal\ctools\Form
View source
abstract class ManageResolverRelationships extends FormBase {
    
    /**
     * The machine name.
     *
     * @var string
     */
    protected $machine_name;
    
    /**
     * An array of property types that are eligible as relationships.
     *
     * @var array
     */
    protected $property_types = [];
    
    /**
     * The typed data resolver.
     *
     * @var \Drupal\ctools\TypedDataResolver
     */
    protected $typedDataResolver;
    
    /**
     * The form builder.
     *
     * @var \Drupal\Core\Form\FormBuilder
     */
    protected $formBuilder;
    
    /**
     * Constructs a new ManageResolverRelationships object.
     *
     * @param \Drupal\ctools\TypedDataResolver $ctools_typed_data_resolver
     *   The typed data resolver.
     * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
     *   The form builder.
     */
    public function __construct(TypedDataResolver $ctools_typed_data_resolver, FormBuilderInterface $form_builder) {
        $this->typedDataResolver = $ctools_typed_data_resolver;
        $this->formBuilder = $form_builder;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('ctools.typed_data.resolver'), $container->get('form_builder'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'ctools_manage_resolver_relationships_form';
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {
        $cached_values = $form_state->getTemporaryValue('wizard');
        $this->machine_name = $cached_values['id'];
        $form['items'] = [
            '#type' => 'markup',
            '#prefix' => '<div id="configured-relationships">',
            '#suffix' => '</div>',
            '#theme' => 'table',
            '#header' => [
                $this->t('Context ID'),
                $this->t('Label'),
                $this->t('Data Type'),
                $this->t('Options'),
            ],
            '#rows' => $this->renderRows($cached_values),
            '#empty' => $this->t('No relationships have been added.'),
        ];
        $form['relationships'] = [
            '#type' => 'select',
            '#title' => $this->t('Add a relationship'),
            '#options' => $this->getAvailableRelationships($cached_values),
        ];
        $form['add_relationship'] = [
            '#type' => 'submit',
            '#name' => 'add',
            '#value' => $this->t('Add Relationship'),
            '#ajax' => [
                'callback' => [
                    $this,
                    'addRelationship',
                ],
                'event' => 'click',
            ],
            '#submit' => [
                'callback' => [
                    $this,
                    'submitForm',
                ],
            ],
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        if ($form_state->getTriggeringElement()['#name'] == 'add') {
            $cached_values = $form_state->getTemporaryValue('wizard');
            [
                ,
                $route_parameters,
            ] = $this->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('relationships'));
            $form_state->setRedirect($this->getAddRoute($cached_values), $route_parameters);
        }
    }
    
    /**
     * Add relationship.
     *
     * @param array $form
     *   The form.
     * @param \Drupal\Core\Form\FormStateInterface $form_state
     *   Stores information about the state of a form.
     */
    public function addRelationship(array &$form, FormStateInterface $form_state) {
        $relationship = $form_state->getValue('relationships');
        $content = $this->formBuilder
            ->getForm($this->getContextClass(), $relationship, $this->getTempstoreId(), $this->machine_name);
        $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
        $cached_values = $form_state->getTemporaryValue('wizard');
        [
            ,
            $route_parameters,
        ] = $this->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $relationship);
        $route_name = $this->getAddRoute($cached_values);
        $route_options = [
            'query' => [
                FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
            ],
        ];
        $url = Url::fromRoute($route_name, $route_parameters, $route_options);
        $content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $url->toString();
        $response = new AjaxResponse();
        $response->addCommand(new OpenModalDialogCommand($this->t('Configure Relationship'), $content, [
            'width' => '700',
        ]));
        return $response;
    }
    
    /**
     * Get the accesssible relationships.
     *
     * @param mixed $cached_values
     *   The arbitrary value from temporary storage.
     */
    protected function getAvailableRelationships($cached_values) {
        
        /** @var \Drupal\ctools\TypedDataResolver $resolver */
        $resolver = $this->typedDataResolver;
        return $resolver->getTokensForContexts($this->getContexts($cached_values));
    }
    
    /**
     * Render the rows.
     *
     * @param mixed $cached_values
     *   The arbitrary value from temporary storage.
     *
     * @return array
     *   The array context.
     */
    protected function renderRows($cached_values) {
        $contexts = [];
        foreach ($this->getContexts($cached_values) as $row => $context) {
            [
                $route_name,
                $route_parameters,
            ] = $this->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $row);
            $build = [
                '#type' => 'operations',
                '#links' => $this->getOperations($cached_values, $row, $route_name, $route_parameters),
            ];
            $contexts[$row] = [
                $row,
                $context->getContextDefinition()
                    ->getLabel(),
                $context->getContextDefinition()
                    ->getDataType(),
                'operations' => [
                    'data' => $build,
                ],
            ];
        }
        return $contexts;
    }
    
    /**
     * Get the operations.
     *
     * @param mixed $cached_values
     *   The arbitrary value from temporary storage.
     * @param string $row
     *   The row.
     * @param string $route_name_base
     *   The base of route.
     * @param array $route_parameters
     *   The parameters of route.
     *
     * @return mixed
     *   The operations.
     */
    protected function getOperations($cached_values, $row, $route_name_base, array $route_parameters = []) {
        // Base contexts will not be a :
        // separated and generated relationships should have 3 parts.
        if (count(explode(':', $row)) < 2) {
            return [];
        }
        $operations['edit'] = [
            'title' => $this->t('Edit'),
            'url' => new Url($route_name_base . '.edit', $route_parameters),
            'weight' => 10,
            'attributes' => [
                'class' => [
                    'use-ajax',
                ],
                'data-dialog-type' => 'modal',
                'data-dialog-options' => Json::encode([
                    'width' => 700,
                ]),
            ],
        ];
        $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-dialog-type' => 'modal',
                'data-dialog-options' => Json::encode([
                    'width' => 700,
                ]),
            ],
        ];
        return $operations;
    }
    
    /**
     * Return a subclass of '\Drupal\ctools\Form\ResolverRelationshipConfigure'.
     *
     * The ConditionConfigure class is designed to be subclassed with custom
     * route information to control the modal/redirect needs of your use case.
     *
     * @return string
     *   Return a subclass of '\Drupal\ctools\Form\ResolverRelationshipConfigure'.
     */
    protected abstract function getContextClass($cached_values);
    
    /**
     * The route to which relationship 'add' actions should submit.
     *
     * @param mixed $cached_values
     *   The arbitrary value from temporary storage.
     *
     * @return string
     *   The route of add action.
     */
    protected abstract function getAddRoute($cached_values);
    
    /**
     * Provide the tempstore id for your specified use case.
     *
     * @return string
     *   The id of tempstore.
     */
    protected abstract function getTempstoreId();
    
    /**
     * Gets the context value.
     *
     * @param mixed $cached_values
     *   The arbitrary value from temporary storage.
     *
     * @return \Drupal\Core\Plugin\Context\ContextInterface[]
     *   Context data and definitions for plugins supporting
     *    caching and return docs.
     */
    protected abstract function getContexts($cached_values);
    
    /**
     * Get relationship operations of route info.
     *
     * @param mixed $cached_values
     *   The arbitrary value from temporary storage.
     * @param string $machine_name
     *   The machine name.
     * @param string $row
     *   The row.
     *
     * @return array
     *   The array of relationship operations.
     */
    protected abstract function getRelationshipOperationsRouteInfo($cached_values, $machine_name, $row);

}

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.
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 73
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.
ManageResolverRelationships::$formBuilder protected property The form builder.
ManageResolverRelationships::$machine_name protected property The machine name.
ManageResolverRelationships::$property_types protected property An array of property types that are eligible as relationships.
ManageResolverRelationships::$typedDataResolver protected property The typed data resolver.
ManageResolverRelationships::addRelationship public function Add relationship.
ManageResolverRelationships::buildForm public function Form constructor. Overrides FormInterface::buildForm
ManageResolverRelationships::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ManageResolverRelationships::getAddRoute abstract protected function The route to which relationship &#039;add&#039; actions should submit.
ManageResolverRelationships::getAvailableRelationships protected function Get the accesssible relationships.
ManageResolverRelationships::getContextClass abstract protected function Return a subclass of &#039;\Drupal\ctools\Form\ResolverRelationshipConfigure&#039;.
ManageResolverRelationships::getContexts abstract protected function Gets the context value.
ManageResolverRelationships::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ManageResolverRelationships::getOperations protected function Get the operations.
ManageResolverRelationships::getRelationshipOperationsRouteInfo abstract protected function Get relationship operations of route info.
ManageResolverRelationships::getTempstoreId abstract protected function Provide the tempstore id for your specified use case.
ManageResolverRelationships::renderRows protected function Render the rows.
ManageResolverRelationships::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ManageResolverRelationships::__construct public function Constructs a new ManageResolverRelationships object.
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. 1
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.
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.