I don't think this was being done before, but might have missed it.

I've made a few settings plugins for my own use cases, things like 'sticky to top' and 'hide exposed form' etc, having the plugin interface is really good.

I found adding the parent field to the view very handy. From there you can get parent entity, which in my case is a paragraph, and therefore all the other sibling fields. This data/context can be useful.

I added :

'field' => $item,

to

      $view->element['#viewsreference'] = [
        'field' => $item,
        'data' => $data,
        'enabled_settings' => array_filter($this->getFieldSetting('enabled_settings')),
      ];

in viewsreference/src/Plugin/Field/FieldFormatter/ViewsReferenceFieldFormatter.php

This allows me to access the parent field and entity in my plugin's alterView function using :

 $parent = $view->element['#viewsreference']['field']->getEntity();

I've attached a patch that I'm applying to the "2.x-dev" version in composer.

Might be useful to someone else.

Comments

8bitplateau created an issue. See original summary.

seanb’s picture

Status: Active » Needs review
StatusFileSize
new2.88 KB

Thanks for the issue. This seems to make a lot of sense to help with view alters and theming. Attached is a reroll on top of the latest dev version.
Since we are now using a lazy builder which passes data also via requests, I chose to pass the entity type, entity ID and field name of the parent.

  • seanB authored 810f1b3b on 8.x-2.x
    Issue #3090713 by 8bitplateau, seanB: Parent field entity in plugin...
seanb’s picture

Status: Needs review » Fixed

Committed the change to 2.x.

Status: Fixed » Closed (fixed)

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