\Comment\Controller\ComponentCommentComponent

Manages entity's comments.

You must use this Component in combination with Commentable behavior and CommentHelper. CommentHelper is automatically attached to your controller when this component is attached.

When this component is attached you can render entity's comments using the CommentHelper:

// in any view:
$this->Comment->config('visibility', 1);
$this->Comment->render($entity);

// in any controller
$this->Comment->config('visibility', 1);

You can set visibility using this component at controller side, or using CommentHelper as example above, accepted values are:

  • 0: Closed; can't post new comments nor read existing ones. (by default)
  • 1: Read & Write; can post new comments and read existing ones.
  • 2: Read Only; can't post new comments but can read existing ones.

Summary

Methods
Properties
Constants
__construct()
beforeFilter()
beforeRender()
config()
post()
No public properties found
No constants found
_akismetStatus()
_afterSave()
_getRequestData()
_getTableAlias()
_setErrors()
_loadSettings()
_createValidator()
$_controller
$_defaultConfig
N/A
No private methods found
No private properties found
N/A

Properties

$_controller

$_controller : \Cake\Controller\Controller

The controller this component is attached to.

Type

\Cake\Controller\Controller

$_defaultConfig

$_defaultConfig : array

Default configuration.

  • redirectOnSuccess: Set to true to redirect to referer page on success. Set to false for no redirection, or set to an array|string compatible with Controller::redirect() method.

  • successMessage: Custom success alert-message. Or a callable method which must return a customized message.

  • errorMessage: Custom error alert-message. Or a callable method which must return a customized message.

  • arrayContext: Information for the ArrayContext provider used by FormHelper when rendering comments form.

  • validator: A custom validator object, if not provided it automatically creates one for you using the information below:

  • settings: Array of additional settings parameters, will be merged with those coming from Comment Plugin's configuration panel (at backend).

When defining successMessage or errorMessage as callable functions you should expect two arguments. A comment entity as first argument and the controller instance this component is attached to as second argument:

$options['successMessage'] = function ($comment, $controller) {
    return 'My customized success message';
}

$options['errorMessage'] = function ($comment, $controller) {
    return 'My customized error message';
}

Type

array

Methods

__construct()

__construct(\Cake\Controller\ComponentRegistry $collection, array $config)

Constructor.

Parameters

\Cake\Controller\ComponentRegistry $collection

A ComponentRegistry for this component

array $config

Array of configuration options to merge with defaults

beforeFilter()

beforeFilter(\Cake\Event\Event $event) : void

Called before the controller's beforeFilter method.

Parameters

\Cake\Event\Event $event

The event that was triggered

beforeRender()

beforeRender(\Cake\Event\Event $event) : void

Called after the controller executes the requested action.

Parameters

\Cake\Event\Event $event

The event that was triggered

config()

config(string|array|null $key, mixed|null $value, boolean $merge) : mixed

Reads/writes settings for this component or for CommentHelper class.

Parameters

string|array|null $key

The key to get/set, or a complete array of configs.

mixed|null $value

The value to set.

boolean $merge

Whether to merge or overwrite existing config, defaults to true.

Throws

\Cake\Core\Exception\Exception

When trying to set a key that is invalid.

Returns

mixed —

Config value being read, or the object itself on write operations.

post()

post(\Cake\Datasource\EntityInterface $entity) : boolean

Adds a new comment for the given entity.

Parameters

\Cake\Datasource\EntityInterface $entity

The entity where to attach new comment

Returns

boolean —

True on success, false otherwise

_akismetStatus()

_akismetStatus(array $data) : string

Calculates comment's status using akismet.

Parameters

array $data

Comment's data to be validated by Akismet

Returns

string —

Filtered comment's status

_afterSave()

_afterSave(\Cake\Datasource\EntityInterface $comment) : void

Logic triggered after comment was successfully saved.

Parameters

\Cake\Datasource\EntityInterface $comment

Comment that was just saved

_getRequestData()

_getRequestData(\Cake\Datasource\EntityInterface $entity) : array

Extract data from request and prepares for inserting a new comment for the given entity.

Parameters

\Cake\Datasource\EntityInterface $entity

Entity used to guess table name

Returns

array

_getTableAlias()

_getTableAlias(\Cake\Datasource\EntityInterface $entity) : string

Get table alias for the given entity.

Parameters

\Cake\Datasource\EntityInterface $entity

The entity

Returns

string —

Table alias

_setErrors()

_setErrors(\Comment\Model\Entity\Comment $comment) : void

Prepares error messages for FormHelper.

Parameters

\Comment\Model\Entity\Comment $comment

The invalidated comment entity to extract error messages

_loadSettings()

_loadSettings() : array

Fetch settings from data base and merges with this component's configuration.

Returns

array

_createValidator()

_createValidator() : \Cake\Validation\Validator

Creates a validation object on the fly.

Returns

\Cake\Validation\Validator