views_form() is the only form that still depends on hook_forms() in core, because it is still a function. By converting it to a class, we can fix #2110951: Remove hook_forms().

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Status: Active » Needs review
Issue tags: +VDC
FileSize
17.28 KB

Let's see how this works out.

Xano’s picture

Assigned: Unassigned » Xano
Status: Needs review » Needs work
  1. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -0,0 +1,135 @@
    + * possible for modules to have a multistep form if they need to.
    

    I know this is a 1:1 copy-paste from the function docblock, but IMHO we should change this. It's even worse for the class than it was for the function.

  2. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -0,0 +1,135 @@
    +  protected $request;
    +
    

    Needs docblock.

  3. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -0,0 +1,135 @@
    +  public function __construct(ControllerResolverInterface $controller_resolver, UrlGeneratorInterface $url_generator, Request $request, $view_id, $display_id) {
    

    Needs docblock.

  4. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -0,0 +1,135 @@
    +    $this->displayId = $display_id;
    

    Undocumented properties.

  5. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -0,0 +1,135 @@
    +      $this->displayId,
    

    Undocumented properties.

  6. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -0,0 +1,135 @@
    +    $form_arg = isset($form_state['step_controller'][$form_state['step']]) ? $form_state['step_controller'][$form_state['step']] : 'Drupal\views\Form\ViewsFormMainForm';
    

    We should use a more descriptive name than $form_arg.

Xano’s picture

Assigned: Xano » Unassigned
Status: Needs work » Needs review
FileSize
18.37 KB
4.11 KB
dawehner’s picture

  1. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -17,33 +17,76 @@
    +   *
    +   * @param ControllerResolverInterface $controller_resolver
    +   * @param UrlGeneratorInterface $url_generator
    +   * @param Request $request
    +   * @param string $view_id
    +   * @param string $view_display_id
    

    Some absolute documentation on there would be kind of cool

  2. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -118,18 +161,18 @@
    +    $form_step_class = isset($form_state['step_controller'][$form_state['step']]) ? $form_state['step_controller'][$form_state['step']] : 'Drupal\views\Form\ViewsFormMainForm';
    

    I like the new variable name.

Xano’s picture

FileSize
7.87 KB
10.02 KB
Xano’s picture

Status: Needs review » Needs work

Whoops, let me fix those files.

Xano’s picture

Status: Needs work » Needs review
FileSize
18.46 KB
748 bytes
dawehner’s picture

FileSize
2.47 KB
18.59 KB

Made some little changes given that I don't really want to RTBC my own patch.

Xano’s picture

Status: Needs review » Reviewed & tested by the community

It's a beauty!

damiankloip’s picture

+++ b/core/modules/views/views.theme.inc
@@ -134,7 +135,9 @@ function template_preprocess_views_view(&$variables) {
+    $form_object = ViewsForm::create(\Drupal::getContainer(), $view->storage->id(), $view->current_display);

Sorry, if we are creating an instance ourselves here, I think we should be creating a new object (new > __construct) instead of using the static create() method.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Needs work due to #10

dawehner’s picture

Status: Needs work » Needs review
FileSize
909 bytes
18.7 KB

.

tim.plunkett’s picture

  1. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -0,0 +1,183 @@
    +  public static function create(ContainerInterface $container, $view_id = NULL, $view_display_id = NULL) {
    

    This seems wrong to me.

  2. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -0,0 +1,183 @@
    +  public function getFormID() {
    +    $parts = array(
    +      'views_form',
    +      $this->viewId,
    +      $this->viewDisplayId,
    +    );
    +
    +    return implode('_', $parts);
    +  }
    

    But I guess we need it here...

  3. +++ b/core/modules/views/lib/Drupal/views/Form/ViewsForm.php
    @@ -0,0 +1,183 @@
    +    if (class_exists($form_step_class)) {
    +      if (in_array('Drupal\Core\DependencyInjection\ContainerInjectionInterface', class_implements($form_step_class))) {
    +        return $form_step_class::create($container);
    +      }
    +
    +      return new $form_step_class();
    +    }
    +
    +    // Otherwise, it is a service.
    +    return $container->get($form_step_class);
    +  }
    

    Ouch, it hurts to have this duplicated again

  4. +++ b/core/modules/views/views.theme.inc
    @@ -134,7 +135,10 @@ function template_preprocess_views_view(&$variables) {
    +    $container = \Drupal::getContainer();
    +    $form_object = new ViewsForm($container->get('controller_resolver'), $container->get('url_generator'), $container->get('request'), $view->storage->id(), $view->current_display);
    

    Why not the ::create here?

  5. +++ b/core/modules/views/views.theme.inc
    @@ -134,7 +135,10 @@ function template_preprocess_views_view(&$variables) {
    +    $form = drupal_get_form($form_object, $view, $output);
    

    FormBuilder!

dawehner’s picture

FileSize
18.72 KB
839 bytes

Why not the ::create here?

See https://drupal.org/node/2110953#comment-7984973

FormBuilder!

Fixed

damiankloip’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/views/lib/Drupal/views/Form/ViewsFormMainForm.php
@@ -0,0 +1,152 @@
+        $field->views_form_validate($form, $form_state);

ughs, we didn't convert that either. I will file a follow up. That got missed in the epic camalization patches.

Patch looks ready though.

damiankloip’s picture

Related, from #15 comment: #2123843: Camelize views form methods

Xano’s picture

14: vdc-2110953-14.patch queued for re-testing.

webchick’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed
Issue tags: +Approved API change

I checked w/ Tim on whether or not we needed to retain legacy wrappers on these functions, but he said that these are all internal-facing Views things, so this looks like a nice clean-up (and one that other modules might be able to use for general wizards with a little tweaking). Tagging as an approved API change.

I'll confess I don't follow all of this code, but in comparing/contrasting the procedural vs. OO hunks, this seems to be basically doing the same thing.

Committed and pushed to 8.x, with some minor docs massaging on the ViewsForm class for the docs gate.

hansfn’s picture

As the maintainer of Views Send, a Views Form implementation, I would very much like a change record for this issue because: 1) It took me quite some time to find this issue and 2) it isn't very clear how you should implement a Views Form now. Thx for any help in making my module work again.

Added some hours later: I was able to make my module work again, but I'm not sure if I did it the right way.

dawehner’s picture

Title: Convert views_forms() to a classed form » Change record: Convert views_forms() to a classed form
Assigned: Unassigned » dawehner
Status: Fixed » Active
Issue tags: +Needs change record
xjm’s picture

Project: Drupal core » Views (for Drupal 7)
Version: 8.x-dev » 8.x-3.x-dev
Component: views.module » Code

Looks like we missed moving this to Views for the change record. :)

Chris Matthews’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.7.x-dev
Component: Code » views.module
Assigned: dawehner » Unassigned

For more information as to why this issue was moved to the Drupal core project, please see issue #3030347: Plan to clean process issue queue

Chris Matthews’s picture

Project: Drupal core » Views (for Drupal 7)
Version: 8.7.x-dev » 7.x-3.x-dev
Component: views.module » Code
Status: Active » Closed (outdated)

Moving back to the contributed Views issue queue and closing as outdated per https://www.drupal.org/project/views/issues/3030347#comment-13023447