block creation and available object?

Permalink
Hi guys

Trying to understand how to create new blocks and what objs are available

I've read this:
http://documentation.concrete5.org/developers/working-with-blocks/c...

But I can't find anywhere that tells me what objects are made available.

For example, in the following code which I've borrowed from a block, in edit.php
there is this;

<div class="form-group">
<?php    echo $form->label('label', t('some text')); ?> 
   <div class="input-group">
      <?php    echo $form->text('some text', $var); ?>
      <div class="input-group-addon">text</div>
   </div>
</div>



How did the author know that '$form' obj was available?

Where can I find the info that tells me what obj are available when creating blocks and how any data is passed?

Hope you understand what I'm trying to explain because i'm a bit lost;)

LT

lowtech
 
WillemAnchor replied on at Permalink Reply
WillemAnchor
Hi Lowtech,

Not all documentation is ready yet. It's a work in progress, and taken seriously. One can tell by the job opening specially for this c5 just published.

Actually, your question is about the same as "what is available in concrete5".
It depends on what you made available in your controller, or what you load later by ajax.
Maybe you can be more specific to what you need or what you want to acccomplish.
lowtech replied on at Permalink Reply
lowtech
Hi WillemAnchor

I'm just trying to learn to build blocks, but i'm a little confused.

I thought this was a specific question

"How did the author know that '$form' obj was available?"

The author did not instantiate the $form object but he knew he had methods available to him. ie $form->label()

Where did he find that info? I can't seem to find it. It must be published somewhere otherwise how would he know.

I also guess DATA is passed in an 'args' array but since the form has no name value pairs is it class="input-group" etc that determines what is added to the array?

I don't know where to find info on that either.

Can you see my confusion? It should be simple but to me right now, it's not ;(

LT

ps
the block i'm looking at as an example is simple enough -- and what I posted is all that is contained in the edit.php
WillemAnchor replied on at Permalink Reply
WillemAnchor
'It must be published somewhere otherwise how would he know.'
The author of those docs also wrote most of the source...so he knows...

Ok, lets get you a bit more on your way:
http://documentation.concrete5.org/developers/working-with-blocks/c... :
When the edit template is loaded, the edit() method from the Controller is run (if it exists), giving a developer the opportunity to inject data into the edit template from the method. Then, the edit.php file is rendered. Additionally, the value for any column on the $btTable database table is automatically injected into the edit.php local scope. This means that if your $bTable has a column entitled "content" and the value of that content for that block is "This is my content" you can simply do this in view.php
lowtech replied on at Permalink Reply
lowtech
Hi

Thanks for trying to help.

I think you may have missed the link I posted at start. I have also read the sub links ie the link you posted. But unfortunately I couldn't find an answer.


"The author of those docs also wrote most of the source...so he knows..."

I'm not sure about that. Here is the source code -- it's not much as you can see.

My only issue here is where does he get $form obj from? That's what i'm trying to understand. Is it made available via C5?

Hence, i'm a bit lost.

LT

controller.php
namespace Concrete\Package\IgSpacer\Block\BlockName;
use \Concrete\Core\Block\BlockController;
defined('C5_EXECUTE') or die(_("Access Denied."));
class Controller extends BlockController {
   protected $btTable = 'btBlockName';
   protected $btInterfaceWidth = "300";
   protected $btInterfaceHeight = "200"; 
   public function getBlockTypeDescription() {
      return t("Text");
   }
   public function getBlockTypeName() {
      return t("Text");
   }
   function save($data) {
WillemAnchor replied on at Permalink Best Answer Reply
WillemAnchor
Ok, here we go...
just follow the classes
in BlockControler:
$helpers = array('form');

It extends AbstractController where the form helper is loaded and included into $helpers.

Now look in AbstractView.php for getScopeItems()
This one gives you a few more answers...view and controller are defined here as well.

renderViewContents is called from render() that uses the great php funtion extract().
there you go...$form is available.

Ah, that was some nice code to dig into :D
lowtech replied on at Permalink Reply
lowtech
@WillemAnchor

First, thanks for the explanation, your time and effort.

But for me, it's bitter sweet.

While it does help me understand how the form object came into scope, I find it hard to believe that the author of the block had to dig around in the C5's core code before working out how to write a simple block.

I doubt I could do or would want to do that.

I'm just a simple coder with probably (high) intermediate skills with a reasonable understanding of OOP.

If I build a form I simply need to know there is a form object available and that form object has x,y,z methods with some description of how to use them.

For example the author new that $form had a label method ie $form->label(). But even following your guide and digging in the core code I still would not have known about the methods.

I thought that kind of info would be in a C5 document somewhere which is what I was/am searching for.

Anyway for me it's still bitter sweet.

I think I'll look through some more blocks for insight and to try gain a better understanding.

Thanks

LT
MrKDilkington replied on at Permalink Reply
MrKDilkington
@WillemAnchor

Thank you, I didn't know about getScopeItem().

This would be perfect for a Tutorial and/or new documentation.

So this makes $form available in block and single page views?
1. getHelperObjects() gets the array of helper objects
https://github.com/concrete5/concrete5/blob/develop/web/concrete/src...
2. getHelperObjects() is called here in getScopeItems()
https://github.com/concrete5/concrete5/blob/develop/web/concrete/src...
3. getScopeItems() is called within render()
https://github.com/concrete5/concrete5/blob/develop/web/concrete/src...

What I am not sure of, how does protected $helpers = array('form'); in BlockController just get "form"? I ask because getHelperObjects() makes me think it gets all helpers.

I I was looking around for more.
<?php
/* some available objects in block views */
// concrete\src\Block\View\BlockView.php
// in BlockView, three objects are made available
// - $b - block object - Concrete\Core\Block\Block
// - $bt - blockType object - Concrete\Core\Block\BlockType\BlockType
// - $a - area object - Concrete\Core\Area\Area
// public function getScopeItems()
// {
//     $items = parent::getScopeItems();
//     $items['b'] = $this->block;
//     $items['bt'] = $this->blockType;
//     $items['a'] = $this->area;
//     return $items;
// }

<?php
/* some available objects in page views */
// C:\xampp\htdocs\concrete5\concrete\src\Page\View\PageView.php
// in PageView, two objects are made available
// - $c - the current page object - Concrete\Core\Page\Page
// - $theme - the current theme - Concrete\Theme\Elemental\PageTheme
// public function getScopeItems()
// {
//     $items = parent::getScopeItems();
//     $items['c'] = $this->c;
//     $items['theme'] = $this->themeObject;
//     return $items;
// }
// 
// C:\xampp\htdocs\concrete5\concrete\src\View\AbstractView.php


I am interested to find more to add to this list.
WillemAnchor replied on at Permalink Reply
WillemAnchor
\Concrete\Core\Controller\AbstracController:
public function getHelperObjects()
    {
        $helpers = array();
        foreach ($this->helpers as $handle) {
            $h = Core::make('helper/' . $handle);
            $helpers[(str_replace('/', '_', $handle))] = $h;
        }
        return $helpers;
    }


$this->helpers was overridden in BlockController:
protected $helpers = array('form');


In AbstractView.php, getHelperObjects() is called on the current controller (block/single_page...)
For a block $this->helpers will only be ['form'].

I didn't test this, but it looks like you can use this in your own block controllers too ! Like:
protected = array['form', 'form/page_selector'];

If I'm not mistaken, this will make $form_page_selector available in your block's view.
I don't see the direct advantage yet, except that with one glimpse on your controller, you would know what helpers are used. Nicely listed close the namespaces it's using.

So, know it would be great to be able to create your own helpers, and include them in this simple way. But I don't know if this is possible. Maybe some Core::Bind() like in \application\app.php

Here's a nice link on stackoverflow:http://stackoverflow.com/questions/6339150/php-variable-overriding...
lowtech replied on at Permalink Reply
lowtech
@WillemAnchor

Thanks for the detailed info. I finally realised that it is 'helpers' that I needed to know more about. Being new to C5, I've over looked helpers.

This thread "helped join the dots up" for me which is all I was really thinking about at the start.

http://legacy-documentation.concrete5.org/developers/forms/standard...


I don't know where that info is contained in the current documentation. Even though there is a link on that page to 'View Current Documentation', it seems pointless as it just goes to the 'start type page' and not actually to an equivalent page in current documentation.


Anyway, your detailed look and guidance into the source code has helped me gain a deeper insight.

Thanks

LT
MrKDilkington replied on at Permalink Reply
MrKDilkington
@WillemAnchor

"In AbstractView.php, getHelperObjects() is called on the current controller (block/single_page...)"

Okay, that was the part that was missing for me.

Thank you.
lowtech replied on at Permalink Reply
lowtech
@WillemAnchor


ok so now I know what i'm looking for I found the current documentation for 5.7 in appendix Form Widget Reference

This document is great because it links to the API for each given point of interest.

For anyone who hasn't seen it, check it out.

http://documentation.concrete5.org/developers/appendix/form-widget-...


Thanks W.A
WillemAnchor replied on at Permalink Reply
WillemAnchor
Hi lowtech,

Good to see that you found the docs that can help you further.
The following add-on might be of great help:
https://www.concrete5.org/marketplace/addons/exchangecore-developer-...
Especially If you are using phpStorm
lowtech replied on at Permalink Reply
lowtech
hi

thanks for info and link.

much appreciated

LT