How to Display widget on a page using source code

Hi everyone how can I display widget using source code. 
I used the following code 

$top_row1 = elgg_view ('widgets/next_celebrations/content');

$body = elgg_view_layout('content', [

'title' => 'The title',

'content' => $top_row1,

'filter' => false,

]);

echo elgg_view_page('title test', $body);
 

But only the widget content is displayed. I want to display full widget with customize option .

What I am suppose to do ? 

Best regard

  • $top_row1 = elgg_view_layout('widgets', [
        'title' => 'title',
        'content' => elgg_view('widgets/next_celebrations/content'),
        'show_add_widgets' => false,
        'num_columns' => 1,
    ]);
    
    $body = elgg_view_layout('content', [
        'title' => 'The title',
        'content' => $top_row1,
        'filter' => false,
    ]);
    
    echo elgg_view_page('title test', $body);
  • Thanks but still have the same problem.  Only the widget content is displayed. I want to display full widget with customize option

    Best regard

  • Of course, because you should register your widget with context where this widget is allowed http://reference.elgg.org/engine_2lib_2widgets_8php.html#a71d4a1df91d694dcbd0f4ff030cd4e76

    You can not show the full widget as ElggEntity w/o creating of it.

    Example for 'Blog' widget to show it on '/blog/view' page:

    start.php
    
    elgg_register_widget_type('blog', elgg_echo('blog'), elgg_echo('blog:widget:description'), ['blog']);
    
    views\default\resources\blog\view.php
    
    elgg_set_context('blog');
    
    .....
    
    $params['content'] = elgg_view_entity($blog, array('full_view' => true));
    
    // check to see if we should allow comments
    if ($blog->comments_on != 'Off' && $blog->status == 'published') {
        $params['content'] .= elgg_view_comments($blog);
    }
    
    $params['sidebar'] = elgg_view('blog/sidebar', array('page' => $page_type));
    
    elgg_push_context('blog');
    $params['content'] .= elgg_view_layout('widgets', ['num_columns' => 1]);
    elgg_pop_context();
    
    $body = elgg_view_layout('content', $params);
    
    echo elgg_view_page($params['title'], $body);

     

    Note: you can do it easy with Widget Manager plugin (check out 'Extra widget contexts' option).

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking