Currently, View Reference Field only provides the rendered view field formatter. When using with Paragraph experimental form display in Preview mode, the view is always rendered in the backend. It would be better if View Reference has a Label field formatter similar to other entity reference fields.

screenshot

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

sonnykt created an issue. See original summary.

sonnykt’s picture

StatusFileSize
new5.03 KB
sonnykt’s picture

Status: Active » Needs review
sonnykt’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
gargsuchi’s picture

Status: Needs review » Reviewed & tested by the community

Looks Good!
Thanks @sonnykt.

seanb’s picture

Status: Reviewed & tested by the community » Needs work

Thanks for working on this. Here is a quick review of the code. Mostly nits. The access checks for the views edit page seems to be the most important blocker for now.

  1. +++ b/src/Plugin/Field/FieldFormatter/ViewsReferenceLabelFormatter.php
    @@ -0,0 +1,151 @@
    +  public function __construct(string $plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ModuleHandlerInterface $module_handler) {
    

    We usually don't type hint strings.

  2. +++ b/src/Plugin/Field/FieldFormatter/ViewsReferenceLabelFormatter.php
    @@ -0,0 +1,151 @@
    +        'link' => TRUE,
    +      ] + parent::defaultSettings();
    

    Nit: The indentation is wrong on this one.

  3. +++ b/src/Plugin/Field/FieldFormatter/ViewsReferenceLabelFormatter.php
    @@ -0,0 +1,151 @@
    +        $uri = Url::fromUri('internal:/admin/structure/views/view/' . $view_id . '/edit/' . $display_id);
    ...
    +        $uri = Url::fromUri('internal:/admin/structure/views/view/' . $view_id . '/edit/');
    

    Can we use the routes here instead of the internal URI? Besides that these routes are coming from the Views UI module which might not be enabled. I guess the check should apply to this section as well. Maybe just move the whole section inside if ($output_as_link) {?

    Besides that, a user might not have permissions to edit the view. Let's check for that as well.

  4. +++ b/src/Plugin/Field/FieldFormatter/ViewsReferenceLabelFormatter.php
    @@ -0,0 +1,151 @@
    +          // Unset field item attributes since they have been included in the
    +          // formatter output and shouldn't be rendered in the field template.
    

    Can we move this comment above this whole section and change it to something like: Move field item attributes to the link element.

jastraat’s picture

Status: Needs work » Needs review
StatusFileSize
new5.1 KB

Modified the patch to address the comments in #6. Also adjusted the patch to use the administrative label of the view versus the title since in may cases a view might not be displaying a title. This is also more consistent with the field widget UI.

omar alahmed’s picture

Patch in #7 works with me. Thanks

joegraduate’s picture

Status: Needs review » Reviewed & tested by the community

We are using #7 and it works well. This is a great improvement to the module. Would love to see this get committed and included in a release.

seanb’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/src/Plugin/Field/FieldFormatter/ViewsReferenceLabelFormatter.php
@@ -0,0 +1,156 @@
+      if ($output_as_link) {
+        if ($view->setDisplay($display_id)) {
+          $uri = Url::fromUri('internal:/admin/structure/views/view/' . $view_id . '/edit/' . $display_id);
+        }
+        else {
+          $uri = Url::fromUri('internal:/admin/structure/views/view/' . $view_id . '/edit/');
+        }

One thing that still needs work:

Can we use the routes here instead of the internal URI? Besides that these routes are coming from the Views UI module which might not be enabled. I guess the check should apply to this section as well.

Besides that, a user might not have permissions to edit the view. Let's check for that as well.

jastraat’s picture

StatusFileSize
new5.1 KB

The following code from the patch in 7 should address checking that views_ui is enabled and that in order to be linked, the user must have the administer views permission.

if (!$this->moduleHandler->moduleExists('views_ui') ||
   !$user->hasPermission('administer views')) {
  $output_as_link = FALSE;
}

I rerolled the patch from 7 to use routes instead of internal URIs.

jastraat’s picture

Status: Needs work » Needs review
seanb’s picture

The following code from the patch in 7 should address checking that views_ui is enabled and that in order to be linked, the user must have the administer views permission.

You are right, sorry I missed that. I'll try to test this out soon. If someone else has time to give this a go and RTBC it then please feel free to do so.

sonnykt’s picture

Created an MR using patch #11 and updated the new formatter to use proper dependency injection.

kristen pol’s picture

Diff of MR and patch in #11.

6c6
< @@ -0,0 +1,165 @@
---
> @@ -0,0 +1,156 @@
13c13
< +use Drupal\Core\Session\AccountInterface;
---
> +use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
15d14
< +use Drupal\views\ViewExecutable;
32c31
< +class ViewsReferenceLabelFormatter extends FormatterBase {
---
> +class ViewsReferenceLabelFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
39,46c38
< +  protected ModuleHandlerInterface $moduleHandler;
< +
< +  /**
< +   * Current user.
< +   *
< +   * @var \Drupal\Core\Session\AccountInterface
< +   */
< +  protected AccountInterface $currentUser;
---
> +  protected $moduleHandler;
51c43
< +  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
---
> +  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ModuleHandlerInterface $module_handler) {
54d45
< +    $this->currentUser = $current_user;
69,70c60
< +      $container->get('module_handler'),
< +      $container->get('current_user')
---
> +      $container->get('module_handler')
108a99
> +    $user = \Drupal::currentUser();
111c102
< +      !$this->currentUser->hasPermission('administer views')) {
---
> +      !$user->hasPermission('administer views')) {
120c111
< +      if (!($view instanceof ViewExecutable)) {
---
> +      if (!is_object($view)) {
samuhe’s picture

Seems like patch in 11 is working correctly. Thank you for the patch.

trackleft2 made their first commit to this issue’s fork.

trackleft2’s picture

Fixing PHPCS issues:

FILE: ...e-2938433/src/Plugin/Field/FieldFormatter/ViewsReferenceLabelFormatter.php
--------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
--------------------------------------------------------------------------------
 72 | ERROR | [x] There should be no white space after an opening "["
    |       |     (Drupal.WhiteSpace.OpenBracketSpacing.OpeningWhitespace)
 72 | ERROR | [x] There should be no white space before a closing "]"
    |       |     (Drupal.WhiteSpace.CloseBracketSpacing.ClosingWhitespace)
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------
Time: 399ms; Memory: 8MB
PHP CODE SNIFFER REPORT SUMMARY
--------------------------------------------------------------------------------
FILE                                                            ERRORS  WARNINGS
--------------------------------------------------------------------------------
...lugin/Field/FieldFormatter/ViewsReferenceLabelFormatter.php  2       0
--------------------------------------------------------------------------------
A TOTAL OF 2 ERRORS AND 0 WARNINGS WERE FOUND IN 29 FILES
--------------------------------------------------------------------------------
PHPCBF CAN FIX 2 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------
PHP CODE SNIFFER VIOLATION SOURCE SUMMARY
----------------------------------------------------------------------
    SOURCE                                                       COUNT
----------------------------------------------------------------------
[x] Drupal.WhiteSpace.CloseBracketSpacing.ClosingWhitespace      1
[x] Drupal.WhiteSpace.OpenBracketSpacing.OpeningWhitespace       1
----------------------------------------------------------------------
A TOTAL OF 2 SNIFF VIOLATIONS WERE FOUND IN 2 SOURCES
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SOURCES AUTOMATICALLY (2 VIOLATIONS IN TOTAL)
----------------------------------------------------------------------
trackleft2’s picture

StatusFileSize
new5.34 KB

Adding static patch with updates.

shortspoken’s picture

Status: Needs review » Reviewed & tested by the community

Marking this RTBC as I am using this patch in several projects successfully. Please merge.

Cheers, Moritz

joegraduate’s picture

Agree with @shortspoken that this is RTBC. Updated the MR branch with the latest commits from 8.x-2.x including test fixes (unrelated to the MR). All pipelines pass on the MR now.

anybody made their first commit to this issue’s fork.

anybody’s picture

Rebased

scott_euser made their first commit to this issue’s fork.

scott_euser’s picture

Status: Reviewed & tested by the community » Needs review

Thanks for the patience with this. I reviewed and found that we had missing schema + fixed some code nitpicks + added test coverage. Given that I did the fixes myself in this, can someone check and move back to RTBC if happy with my changes. Then I'll merge it in. Thanks!

joegraduate’s picture

Status: Needs review » Reviewed & tested by the community

@scott_euser, your latest changes to the MR look good to me. Good catch on the missing config schema and good call on adding test coverage.

anybody’s picture

Nice! This will be very helpful for different kind of previews where viewsreference is used, e.g. layout paragraphs!

Looking very much forward to the next release! 🎉

scott_euser changed the visibility of the branch 8.x-2.x to hidden.

  • scott_euser committed 49b2bc13 on 8.x-2.x authored by sonnykt
    fix: #2938433 Add Label field formatter
    
    By: sonnykt
    By: seanb
    By:...
scott_euser’s picture

Status: Reviewed & tested by the community » Fixed

Thanks everyone!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

anybody’s picture

Thanks @scott_euser this is helpful! Could you also tag a new release with this soon? That would be fantastic!

anybody’s picture

PS: Sorry just saw #3019753: Plan for Views Reference Field 8.x-2.0 release if you don't want to tag another beta release before.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.