权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>Gutenberg : how can I get the title of the current block within its 'edit' function?
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Gutenberg : how can I get the title of the current block within its 'edit' function?

programmeradmin8浏览0评论

I'm a Gutenberg newbie.

I'm trying to display a placeholder component with my block's edit function:

export default function Edit( { attributes, setAttributes, className } ) {
  return (
    <Placeholder
      icon={ icon }
      label={ title }
    >
  );
}

I would like to display the block icon and title, as they have been defined within the function registerBlockType.

Could someone point me how to do this ? Thanks !

I'm a Gutenberg newbie.

I'm trying to display a placeholder component with my block's edit function:

export default function Edit( { attributes, setAttributes, className } ) {
  return (
    <Placeholder
      icon={ icon }
      label={ title }
    >
  );
}

I would like to display the block icon and title, as they have been defined within the function registerBlockType.

Could someone point me how to do this ? Thanks !

Share Improve this question asked Sep 18, 2020 at 21:38 gordiegordie 4925 silver badges19 bronze badges 1
  • If it's just for a block type you created, then you could, for example, just define a constant with the title/icon/etc. (e.g. const BLOCK_TITLE = 'Foo Block') and simply access the constant from the edit function as well as in the root of the registerBlockType(). – Sally CJ Commented Sep 19, 2020 at 7:19
Add a comment  | 

1 Answer 1

Reset to default 1

You can use wp.blocks.getBlockType() to get the block type's settings.

So if you've registered a block type with the name my-guten/foo-block, just run getBlockType( 'my-guten/foo-block' ) to get the block type's title, icon, etc.

const { registerBlockType, getBlockType } = wp.blocks;

// Register a block type.
registerBlockType( 'my-guten/foo-block', { ... } );

// Get the block type settings (title, icon, etc.).
const blockType = getBlockType( 'my-guten/foo-block' );

// Inspect the settings:
console.log( blockType );

And from within a block type's edit function, the block type's name is passed as a property named name in the first function argument which is an object with other properties like attributes and className. So for examples:

// Using a Dashicons icon.
registerBlockType( 'my-guten/foo-block', {
    title: 'Foo Block',
    icon: 'heart',
    category: 'common',
    edit: function ( props ) {
        const blockType = getBlockType( props.name );

        console.log( blockType.title ); // Foo Block

        ... your code.
    },
    ... your code.
} );

// Using an SVG icon. (JSX)
registerBlockType( 'my-guten/foo-block', {
    title: 'Foo Block',
    icon: <svg viewBox="0 0 24 24" xmlns="http://www.w3/2000/svg"><path fill="none" d="M0 0h24v24H0V0z" /><path d="M19 13H5v-2h14v2z" /></svg>,
    category: 'common',
    // Unpack the first argument — so that we can use "name" and not props.name.
    edit: function ( { name } ) {
        const blockType = getBlockType( name );

        ... your code.
    },
    ... your code.
} );

Example based on your code

In edit.js:

const { getBlockType } = wp.blocks;
const { Placeholder } = wpponents;

export default function Edit( { attributes, setAttributes, className, name } ) {
    const blockType = getBlockType( name );

    return (
        <Placeholder
            icon={ blockType.icon.src }
            label={ blockType.title }
        />
    );
}

In index.js:

import Edit from './edit';

const { registerBlockType } = wp.blocks;

registerBlockType( 'my-guten/foo-block', {
    edit: Edit,
    ... your code.
} );
发布评论

评论列表(0)

  1. 暂无评论