diff --git a/src/Form/ManageResolverRelationships.php b/src/Form/ManageResolverRelationships.php index bb6bcfb2ef215122e808d831a69dd4af7d4473ca..993181bb874b2b048ff5353fb3ffe4a2c155684e 100644 --- a/src/Form/ManageResolverRelationships.php +++ b/src/Form/ManageResolverRelationships.php @@ -12,9 +12,14 @@ use Drupal\Core\Url; use Drupal\ctools\TypedDataResolver; use Symfony\Component\DependencyInjection\ContainerInterface; +/** + * Provider manage resolver relationships. + */ abstract class ManageResolverRelationships extends FormBase { /** + * The machine name. + * * @var string */ protected $machine_name; @@ -76,15 +81,17 @@ abstract class ManageResolverRelationships extends FormBase { public function buildForm(array $form, FormStateInterface $form_state) { $cached_values = $form_state->getTemporaryValue('wizard'); $this->machine_name = $cached_values['id']; - $form['items'] = array( + $form['items'] = [ '#type' => 'markup', '#prefix' => '<div id="configured-relationships">', '#suffix' => '</div>', '#theme' => 'table', - '#header' => array($this->t('Context ID'), $this->t('Label'), $this->t('Data Type'), $this->t('Options')), + '#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.') - ); + '#empty' => $this->t('No relationships have been added.'), + ]; $form['relationships'] = [ '#type' => 'select', @@ -101,7 +108,7 @@ abstract class ManageResolverRelationships extends FormBase { ], '#submit' => [ 'callback' => [$this, 'submitForm'], - ] + ], ]; return $form; } @@ -117,6 +124,14 @@ abstract class ManageResolverRelationships extends FormBase { } } + /** + * 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); @@ -132,10 +147,16 @@ abstract class ManageResolverRelationships extends FormBase { $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, array('width' => '700'))); + $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; @@ -143,68 +164,80 @@ abstract class ManageResolverRelationships extends FormBase { } /** - * @param $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 = array(); + $contexts = []; foreach ($this->getContexts($cached_values) as $row => $context) { list($route_name, $route_parameters) = $this->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $row); - $build = array( + $build = [ '#type' => 'operations', '#links' => $this->getOperations($cached_values, $row, $route_name, $route_parameters), - ); - $contexts[$row] = array( + ]; + $contexts[$row] = [ $row, $context->getContextDefinition()->getLabel(), $context->getContextDefinition()->getDataType(), 'operations' => [ 'data' => $build, ], - ); + ]; } return $contexts; } /** - * @param array $cached_values + * 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 paramenters of route. * * @return mixed + * The operations. */ - protected function getOperations($cached_values, $row, $route_name_base, array $route_parameters = array()) { - // Base contexts will not be a : separated and generated relationships should have 3 parts. + 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'] = array( + $operations['edit'] = [ 'title' => $this->t('Edit'), 'url' => new Url($route_name_base . '.edit', $route_parameters), 'weight' => 10, - 'attributes' => array( + 'attributes' => [ 'class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode([ 'width' => 700, ]), - ), - ); + ], + ]; $route_parameters['id'] = $route_parameters['context']; - $operations['delete'] = array( + $operations['delete'] = [ 'title' => $this->t('Delete'), 'url' => new Url($route_name_base . '.delete', $route_parameters), 'weight' => 100, - 'attributes' => array( - 'class' => array('use-ajax'), + 'attributes' => [ + 'class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode([ 'width' => 700, ]), - ), - ); + ], + ]; return $operations; } @@ -215,13 +248,18 @@ abstract class ManageResolverRelationships extends FormBase { * route information to control the modal/redirect needs of your use case. * * @return string + * Return a subclass of '\Drupal\ctools\Form\ResolverRelationshipConfigure'. */ abstract protected 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. */ abstract protected function getAddRoute($cached_values); @@ -229,22 +267,34 @@ abstract class ManageResolverRelationships extends FormBase { * Provide the tempstore id for your specified use case. * * @return string + * The id of tempstore. */ abstract protected function getTempstoreId(); /** - * @param $cached_values + * 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. */ abstract protected function getContexts($cached_values); /** - * @param string $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. */ abstract protected function getRelationshipOperationsRouteInfo($cached_values, $machine_name, $row);