I'm looking to add a plugin for Views to provide some custom functionality in the footer of a view.

First, I created the following in my_module/src/Plugin/views/area/MyPlugin.php:

namespace Drupal\my_module\Plugin\views\area;

use Drupal\views\Plugin\views\area\AreaPluginBase;

/**
 * Defines a views area plugin.
 *
 * @ingroup views_area_handlers
 *
 * @ViewsArea("my_plugin")
 */
class MyPlugin extends AreaPluginBase {

  /**
   * {@inheritdoc}
   */
  public function render($empty = FALSE) {
    if (!$empty || !empty($this->options['empty'])) {
      return array(
        '#markup' => 'test footer content',
      );
    }

    return array();
  }
}

When that didn't work on its own, I realized I needed to declare the plugin in my_module.views.inc:

/**
 * Implements hook_views_data().
 */
function my_module_views_data() {
  $data['views']['my_plugin'] = array(
    'title' => t('My Plugin'),
    'help' => t('My plugin description.'),
    'area' => array(
      'id' => 'my_plugin',
    ),
  );
  return $data;
}

With that added, I can get as far as seeing the plugin available for selection in an area under the Views admin screen. However, when I select the checkbox for it and click Apply, the button text changes to "Add and configure footer" but it doesn't move to the next step in the process.

I've had this happen when writing custom block plugins; the system don't let me place a block when the plugin has something wrong with it. Any thoughts as to what I have here that is wrong?

  • 1
    Open the chrome inspector. Under network you will be able to see the returned ajax call. It probably returned an error. If so, can you add it? – Eyal Jun 8 '16 at 6:08
  • 1
    I have just tested your code and it works, I am not able to reproduce the problem. Perhaps you have a typo in the namespace or somewhere else? One way to troubleshoot it is to check your webserver (apache) error logs, as well as investigating the Ajax response, as per previous comment. – marcoscano Jun 8 '16 at 9:31
  • Thanks Eyal and marcoscano, the Ajax error showed me where my typo was. – akalata Jun 8 '16 at 15:31

It looks like you need to declare the plugin in a views.schema.yml file (in this case, in config/schema/my_module.views.schema.yml, relative to your module root):

views.area.my_plugin:
  type: views_area
  label: 'My Plugin'
  • I've added the views.schema.yml (uninstalled and reinstalled the module so it would pick up), but that didn't change the current behavior. I also don't think that the *.views.schema.yml is required -- the Calendar module provides its own area, but doesn't use a views.schema file. – akalata Jun 8 '16 at 15:15
up vote 0 down vote accepted

There was a typo in my plugin filename. :-/

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.