Creating custom blocks and templates does not work in Concrete 5.7

Permalink 1 user found helpful
Yes, I cleared my cache and turned it off completely.

I copied siteroot/concrete/blocks to siteroot/blocks but changing things there doesn't do anything and I don't have any blocks listed as waiting for installation.

I tried it on a different machines, checked the permissions a million times, checked whether I have all requirements and everything.

I've been trying to get this to work for almost 2 weeks now. I'm willing to give you my server credentials, if you can log-on and show me what's wrong. If I can't get this to work soon, I'll have to drop concrete5...

 
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
Hi qaghan,

"I copied siteroot/concrete/blocks to siteroot/blocks but changing things there doesn't do anything and I don't have any blocks listed as waiting for installation."

The first problem is that you should be copying the individual blocks into:
application\blocks

You currently have a folder in the site root called "blocks" that won't do anything.

Concrete5 looks in the application\blocks folder for custom block templates, custom blocks, and for core block overrides.

I strongly recommend that you do not copy all the core blocks into application\blocks at once. You will be overwhelmed by namespace errors which will have to be corrected block by block.

Here is an example for overriding a core block:

Let's override the testimonial block.

1. copy the testimonial folder from:
concrete\blocks
2. paste the testimonial folder into:
application\blocks
3. go to Dashboard> Stacks & Blocks > Block Types
4. after clicking on Block Types, you will get an error:
An unexpected error occurred.
Class '\Application\Block\Testimonial\Controller' not found

5. to fix this, you need to change the namespace of the testimonial block controller to reflect that it is now in the application folder
6. in the testimonial block folder, open controller.php
7. look for:
namespace Concrete\Block\Testimonial;

change it to
namespace Application\Block\Testimonial;

8. refresh your page or click "Back to Home"

The block has been overridden.


Here is an example of creating a new block from a core block:

Let's use the testimonial block to make a new block.

1. rename the testimonial folder to something else - "product_listing"
2. the name must be lowercase and you can separate words with an underscore
3. in the new product_listing block folder, open controller.php
4. look for:
namespace Application\Block\Testimonial;

change it to
namespace Application\Block\ProductListing;

5. look for these two methods - they set the display name and description of your block:
public function getBlockTypeDescription()
{
    return t("Displays a quote or paragraph next to biographical information and a person's picture.");
}
public function getBlockTypeName()
{
    return t("Testimonial");
}

change to
public function getBlockTypeDescription()
{
    return t("Display product listings and details");
}
public function getBlockTypeName()
{
    return t("Product Listing");
}

6. look for the $btTable property to set the name of your database table:
protected $btTable = 'btTestimonial';

change it to
protected $btTable = 'btProductListing';

7. open db.xml and look for:
<table name="btTestimonial">

change to
<table name="btProductListing">

8. go to Dashboard> Stacks & Blocks > Block Types and you will see a new block waiting for install called "Product Listing"
9. install it
10. put a page in Edit Mode and add a block - you will see a new Product Listing block at the bottom

When you finish your block, you will want to make a new icon for it. A 50px x 50px png called icon.png.
qaghan replied on at Permalink Reply
You are an angel among men!

I don't get why the IRC channel insisted that I have to create a blocks folder in my siteroot and then place the custom (core) blocks in there. I asked the same question on reddit and they said pretty much the same. Nobody even mentioned the namespace conflicts. Days and days of frustration finally resolved, thank you very much. I was almost going to go back to Wordpress (ugh!), because I'm running so late with this project already.

Again, thank you so much.
nutama replied on at Permalink Reply 2 Attachments
Hi,

I'm having problems creating custom blocks under /application directory. I followed your instruction & yet it still does not work.

- I've tried this on concrete57.4 and concrete57.52

Are there any issue in my code (as attached) or is there a bug for creating custom blocks in concrete57 ?

Thank you & regards,
MrKDilkington replied on at Permalink Reply
MrKDilkington
@nutama

The product_listing block you attached installs and works correctly.

At what point do you see the error?
nutama replied on at Permalink Reply
@MrKDilkington

Thanks for the reply,

I finally sorted out the problem on my side.. I saw the error upon installation on add functionality/extend screen (on 5.7)

The issue was inside my controller.. the file uploaded on the server slightly different to the one I have on local machine.

instead having this :
<code>
class Controller extends BlockController
{
....
}
</code>

I had :
<code>
class ProductListingController extends BlockController
{
....
}
</code>

I can install this fine now as a package, however I'm still having problems when uploading the code as a standalone "custom block" placed in '/application/blocks' directory (manual block install).

Is this the new behavior of version 5.7 ? where you can only have custom blocks installed as packages ?

Thank you so much for the help..
MrKDilkington replied on at Permalink Reply
MrKDilkington
@nutama

- I unzip the product_listing.zip file (that you attached in your previous reply) to application/blocks
application\blocks\product_listing
- After unzipping, I navigate to the Block Types page and install "Product Listing"
Dashboard > Stacks & Blocks > Block Types > Product List install button
- I now can return to the website and add the Product Listing block to a page

When you follow these steps, do you get an error? If so, at what step?
nutama replied on at Permalink Reply 1 Attachment
Thanks for the reply..

I did exactly as what you did..
1. unzipped the block code, and put it under /application/blocks/ (application/blocks/product_listing)
2. After unzipping, I navigate to the Dashboard > Stack & Blocks > Block Types
(*at this point I get the error - shown on the picture attached)

*seems like it could not execute the product_listing -controller.php.

Do you have any ideas what might cause this in my 5.7 (v. 5.7.52) installation..?

May I know the concrete5 version you have on your machine..?

Thanks so much for the help..
MrKDilkington replied on at Permalink Reply
MrKDilkington
@nutama

I am not sure why this is happening.

The version of concrete5 shouldn't be an issue, it worked with the version from last year and the most current version.

Are you using a local install?

Have you tried creating another clean install to test on?
nutama replied on at Permalink Reply
@MrKDilkington

Yes, I'm on local install.

I have tried to test this installation on a clean 57.4 before.. I don't think it worked..

But I have changed the code since.. I might give it a try one more time.

Thanks so much @MrKDilkington
Shubham2961995 replied on at Permalink Reply 1 Attachment
Hi,

I follow all your given steps but nothing works for me. I have attachment screenshot of error.
please help me. Thanks a lot in advance.
Gondwana replied on at Permalink Reply
Gondwana
You haven't provided enough info. Paste the relevant bits of your code.

If forced to guess, I'd say you haven't got the capitalisation and punctuation of your class name, filenames and namespaces all correct. Alternatively, you could have one or more files in the wrong directory.
potter4991 replied on at Permalink Reply 1 Attachment
Hi MrKDilkington,

I'm having an issue with bringing over a autonav template from 5.6 to 5.7. I've tried to make heads and tails of it myself, but don't hold the knowledge to make the changes.

When I copy and paste my custom template autonav folder from 5.6 [root]/blocks/ to 5.7 [root]/application/blocks/ and then apply the custom template to an autonav block on my site, I'm returned with a "Class 'BlockViewTemplate' not found" error on my homepage. I have to revert to an old version to get the page working again.

Any Ideas? I've attached my 5.6 template for you to look at.

Many thanks in advance!!
MrKDilkington replied on at Permalink Reply
MrKDilkington
@potter4991

The error is caused because you are trying to create a new BlockViewTemplate object, but PHP doesn't know where that class is or if it exists.

To fix this, you can import the BlockViewTemplate class.
- open view.php
application\blocks\autonav\templates\sooperfish\view.php
- after the opening <?php tag, create a new line and add this
use Concrete\Core\Block\View\BlockViewTemplate;
PJSAndo replied on at Permalink Reply
Great!
I have created a new block type 'Tour Details' (from Testimonials) which is showing up in the dashboard as expected.

I'd now like to enter some custom php into the block so it fetches data and displays it inside the block. I have the php, but where do I need to place it?

Thanks
p
MrKDilkington replied on at Permalink Reply
MrKDilkington
@PJSAndo

You want to use a third party library in your block, correct (TourCMS)?


Check out these two links, I think they will point you in the right direction.
https://www.concrete5.org/community/forums/5-7-discussion/how-do-y5....

https://www.concrete5.org/community/forums/5-7-discussion/loading-ex...
PJSAndo replied on at Permalink Reply
Hi MrKDilkington,

Thanks for this.

I've pretty much got it working. I've got the library installed in root which seems to do the trick.

My problem was that I wasn't sure of the best way to get data into pages. I'm sure there's a better way, but I just inserted the necessary php into php blocks that retrieved that data from the external site. The problem I had though was that I was using a right-sidebar page which meant that I had to drag two php blocks onto the page and make two calls to 'include' config.php etc on the same page (once for the main content and once for the sidebar). This didn't work for some reason. So, I'm using a Full Width page and I've created sidebar using rows/cols/divs - I only need to drag one php block this way and do one 'include' this way. This seems to work.

I'm sure there is a much better way to do this! :)
MrKDilkington replied on at Permalink Reply
MrKDilkington
@PJSAndo

You are inserting PHP directly into your pages with the Php Code Block add-on, correct?
https://www.concrete5.org/marketplace/addons/php-code-block...
PJSAndo replied on at Permalink Reply
Hi MrKDilkingonton,

Yes.

Really, I just need a single page template/type (which includes php that retrieves external data - this bit is ok) that is applied to all single-tour pages. When I make changes to the php in that template/type those changes will need to affect all single-tour pages. For example, if I want to add a new custom data field to all single-tour pages then I want to be able to do this by just changing the php once and having that change applied to all associated pages.

At the moment, I'm developing/testing the php by entering the code into a php block and dragging the block into the Main area of a full page. But in the future I'll probably want this php to be hardwired into some kind of template that I can just apply to newly created pages of type single-tour.

PJSAndo