Extending core gutenberg bock with attributes

Question

so i created a custom plugin that should extend the core/video gutenberg block.
the plan is, to disable video download. so disable the context menu and also add the widly unsupported but anyhow controlsList attribute. following functionality works, means console log reports my added attributes, but they don’t appear on the frontend?

php plugin file:

function bx_enqueue_block_editor_assets()
{
    wp_enqueue_script(
        'block-extensions-script',
        plugins_url('block-extensions.js', __FILE__),
        ['wp-blocks']
    );
}

add_action('enqueue_block_editor_assets', 'bx_enqueue_block_editor_assets');

js plugin file:

const addVideoAttributes = (settings, name) => {
  if (name === 'core/video') {
    if (settings.attributes) {

      settings.attributes.controlsList = {
        type: 'string',
        default: 'nodownload'
      };

      settings.attributes.oncontextmenu = {
        type: 'string',
        default: 'return false'
      };

      console.log('settings', settings);
    }
  }

  return settings;
}

wp.hooks.addFilter(
  'blocks.registerBlockType', 
  'bx/add-video-attributes', 
  addVideoAttributes
);

i also tried the blocks.getBlockAttributes hook, with the exact same luck:

const addVideoAttributes2 = (attributes, block) => {
  if (block.name === 'core/video') {
    attributes.controlsList = 'nodownload';
    attributes.oncontextmenu = 'return false;';
  }

  return attributes;
}

wp.hooks.addFilter(
  'blocks.getBlockAttributes',
  'bx/add-video-controls-list',
  addVideoAttributes2
);
0
honk31 2 months 0 Answers 9 views 0

Leave an answer