ElementIndexesController ​
- Type
- Class
- Namespace
- craft\controllers
- Inherits
- craft\controllers\ElementIndexesController » craft\controllers\BaseElementsController » craft\web\Controller » yii\web\Controller » yii\base\Controller » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable, yii\base\ViewContextInterface
- Since
- 3.0.0
The ElementIndexesController class is a controller that handles various element index related actions.
Note that all actions in the controller require an authenticated Craft session via allowAnonymous.
Public Properties ​
Property | Description |
---|---|
action | yii\base\Action, null – The action that is currently being executed. |
actionParams | array – The parameters bound to the current action. |
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
defaultAction | string – The ID of the action that is used when the action ID is not specified in the request. |
enableCsrfValidation | boolean – Whether to enable CSRF validation for the actions in this controller. |
id | string – The ID of this controller. |
layout | string, null, false – The name of the layout to be applied to this controller's views. |
module | yii\base\Module – The module that this controller belongs to. |
modules | yii\base\Module – All ancestor modules that this controller is located within. |
request | craft\web\Request |
response | craft\web\Response |
route | string – The route (module ID, controller ID and action ID) of the current request. |
uniqueId | string – The controller ID that is prefixed with the module ID (if any). |
view | craft\web\View – The view object that can be used to render views or view files |
viewPath | string – The directory containing the view files for this controller. |
Protected Properties ​
Property | Description |
---|---|
actions | craft\base\ElementActionInterface[], null |
allowAnonymous | integer, boolean, integer[], string[] – Whether this controller’s actions can be accessed anonymously. |
condition | craft\elements\conditions\ElementConditionInterface, null |
context | string |
elementQuery | craft\elements\db\ElementQueryInterface, null |
elementType | class-string<\craft\base\ElementInterface> |
exporters | craft\base\ElementExporterInterface[], null |
source | array, null |
sourceKey | string, null |
unfilteredElementQuery | craft\elements\db\ElementQueryInterface, null |
viewState | array, null |
actions
​
- Type
- craft\base\ElementActionInterface[], null
- Default value
null
condition
​
- Type
- craft\elements\conditions\ElementConditionInterface, null
- Default value
null
- Since
- 4.0.0
context
​
- Type
- string
- Default value
null
elementQuery
​
- Type
- craft\elements\db\ElementQueryInterface, null
- Default value
null
elementType
​
- Type
class-string<\craft\base\ElementInterface>
- Default value
null
exporters
​
- Type
- craft\base\ElementExporterInterface[], null
- Default value
null
source
​
sourceKey
​
unfilteredElementQuery
​
- Type
- craft\elements\db\ElementQueryInterface, null
- Default value
null
- Since
- 5.0.0
viewState
​
Public Methods ​
Method | Description |
---|---|
__call() | Calls the named method which is not a class method. |
__clone() | This method is called after the object is created by cloning an existing one. |
__construct() | |
__get() | Returns the value of a component property. |
__isset() | Checks if a property is set, i.e. defined and not null. |
__set() | Sets the value of a component property. |
__unset() | Sets a component property to be null. |
actionCountElements() | Returns the total number of elements that match the current criteria. |
actionElementTableHtml() | Returns the updated table attribute HTML for an element. |
actionExport() | Exports element data. |
actionFilterHud() | Creates a filter HUD’s contents. |
actionGetElements() | Renders and returns an element index container, plus its first batch of elements. |
actionGetMoreElements() | Renders and returns a subsequent batch of elements for an element index. |
actionGetSourceTreeHtml() | Returns the source tree HTML for an element index. |
actionPerformAction() | Performs an action on one or more selected elements. |
actionSaveElements() | Saves inline-edited elements. |
actionSourcePath() | Returns the source path for the given source key, step key, and context. |
actions() | Declares external actions for the controller. |
afterAction() | This method is invoked right after an action is executed. |
asCpModal() | Sends a control panel modal response. |
asCpScreen() | Sends a control panel screen response. |
asErrorJson() | Responds to the request with a JSON error message. |
asFailure() | Sends a failure response. |
asJson() | Send data formatted as JSON. |
asJsonP() | Sets the response format of the given data as JSONP. |
asModelFailure() | Sends a failure response for a model. |
asModelSuccess() | Sends a success response for a model. |
asRaw() | Sets the response format of the given data as RAW. |
asSuccess() | Sends a success response. |
asXml() | Send data formatted as XML. |
attachBehavior() | Attaches a behavior to this component. |
attachBehaviors() | Attaches a list of behaviors to the component. |
beforeAction() | This method is invoked right before an action is executed. |
behaviors() | Returns a list of behaviors that this component should behave as. |
bindActionParams() | Binds the parameters to the action. |
canGetProperty() | Returns a value indicating whether a property can be read. |
canSetProperty() | Returns a value indicating whether a property can be set. |
className() | Returns the fully qualified name of this class. |
createAction() | Creates an action based on the given action ID. |
currentUser() | Returns the currently logged-in user. |
detachBehavior() | Detaches a behavior from the component. |
detachBehaviors() | Detaches all behaviors from the component. |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. |
findLayoutFile() | Finds the applicable layout file. |
getBehavior() | Returns the named behavior object. |
getBehaviors() | Returns all behaviors attached to this component. |
getElementQuery() | Returns the element query that’s defining which elements will be returned in the current request. |
getModules() | Returns all ancestor modules of this controller. |
getRoute() | Returns the route of the current request. |
getUniqueId() | Returns the unique ID of the controller. |
getView() | Returns the view object that can be used to render views or view files |
getViewPath() | Returns the directory containing view files for this controller. |
goBack() | Redirects the browser to the last visited page. |
goHome() | Redirects the browser to the home page. |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. |
hasMethod() | Returns a value indicating whether a method is defined. |
hasProperty() | Returns a value indicating whether a property is defined for this component. |
init() | Initializes the object. |
off() | Detaches an existing event handler from this component. |
on() | Attaches an event handler to an event. |
redirect() | Redirects the browser to the specified URL. |
redirectToPostedUrl() | Redirects to the URI specified in the POST. |
refresh() | Refreshes the current page. |
render() | Renders a view and applies layout if available. |
renderAjax() | Renders a view in response to an AJAX request. |
renderContent() | Renders a static string by applying a layout. |
renderFile() | Renders a view file. |
renderPartial() | Renders a view without applying layout. |
renderTemplate() | Sends a rendered template response. |
requireAcceptsJson() | Throws a 400 error if the request doesn't accept JSON. |
requireAdmin() | Throws a 403 error if the current user is not an admin. |
requireAuthorization() | Checks whether the current user can perform a given action, and ends the request with a 403 error if they don’t. |
requireCpRequest() | Throws a 400 error if the current request isn’t a control panel request. |
requireElevatedSession() | Requires that the user has an elevated session. |
requireGuest() | Redirects the user to the account template if they are logged in. |
requireLogin() | Redirects the user to the login template if they're not logged in. |
requirePermission() | Checks whether the current user has a given permission, and ends the request with a 403 error if they don’t. |
requirePostRequest() | Throws a 400 error if this isn’t a POST request |
requireSiteRequest() | Throws a 400 error if the current request isn’t a site request. |
requireToken() | Throws a 400 error if the current request doesn’t have a valid Craft token. |
run() | Runs a request specified in terms of a route. |
runAction() | Runs an action within this controller with the specified action ID and parameters. |
setFailFlash() | Sets an error flash message on the user session. |
setSuccessFlash() | Sets a success flash message on the user session. |
setView() | Sets the view object to be used by this controller. |
setViewPath() | Sets the directory that contains the view files. |
trigger() | Triggers an event. |
actionCountElements()
​
- Since
- 3.4.6
Returns the total number of elements that match the current criteria.
Returns ​
actionElementTableHtml()
​
Returns the updated table attribute HTML for an element.
Returns ​
Throws ​
actionExport()
​
- Since
- 3.4.4
Exports element data.
Returns ​
Throws ​
actionFilterHud()
​
- Since
- 4.0.0
Creates a filter HUD’s contents.
actionGetElements()
​
Renders and returns an element index container, plus its first batch of elements.
Returns ​
actionGetMoreElements()
​
Renders and returns a subsequent batch of elements for an element index.
Returns ​
actionGetSourceTreeHtml()
​
Returns the source tree HTML for an element index.
Returns ​
actionPerformAction()
​
Performs an action on one or more selected elements.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if the requested element action is not supported by the element type, or its parameters didn’t validate
actionSaveElements()
​
- Since
- 5.0.0
Saves inline-edited elements.
Returns ​
actionSourcePath()
​
- Since
- 4.4.12
Returns the source path for the given source key, step key, and context.
beforeAction()
​
This method is invoked right before an action is executed.
The method will trigger the EVENT_BEFORE_ACTION event. The return value of the method will determine whether the action should continue to run.
In case the action should not run, the request should be handled inside of the beforeAction
code by either providing the necessary output or redirecting the request. Otherwise the response will be empty.
If you override this method, your code should look like the following:
public function beforeAction($action): bool
{
// your custom code here, if you want the code to run before action filters,
// which are triggered on the [EVENT_BEFORE_ACTION](https://www.yiiframework.com/doc/api/2.0/yii-base-controller#EVENT_BEFORE_ACTION-detail) event, e.g. PageCache or AccessControl
if (!parent::beforeAction($action)) {
return false;
}
// other custom code here
return true; // or false to not run the action
}
Arguments ​
$action
(yii\base\Action) – The action to be executed.
Returns ​
boolean – Whether the action should continue to run.
Throws ​
- yii\web\BadRequestHttpException
if the request is missing a valid CSRF token - yii\web\ForbiddenHttpException
if the user is not logged in or lacks the necessary permissions - craft\web\ServiceUnavailableHttpException
if the system is offline and the user isn't allowed to access it - yii\web\UnauthorizedHttpException
getElementQuery()
​
Returns the element query that’s defining which elements will be returned in the current request.
Other components can fetch this like so:
$criteria = Craft::$app->controller->getElementQuery();
Returns ​
craft\elements\db\ElementQueryInterface
Protected Methods ​
Method | Description |
---|---|
actionData() | Returns the data for the available actions. |
availableActions() | Returns the available actions for the current source. |
availableExporters() | Returns the available exporters for the current source. |
bindInjectedParams() | Fills parameters based on types and names in action method signature. |
condition() | Returns the condition that should be applied to the element query. |
context() | Returns the context that this controller is being called in. |
defineBehaviors() | Returns the behaviors to attach to this class. |
elementQuery() | Returns the element query based on the current params. |
elementResponseData() | Returns the element data to be returned to the client. |
elementType() | Returns the posted element type class. |
exporterData() | Returns the data for the available exporters. |
getPostedRedirectUrl() | Gets the redirect param specified in the POST data. |
isAdministrative() | Returns whether the element index has an administrative context (index or embedded-index ). |
source() | Returns the selected source info. |
viewState() | Returns the current view state. |
actionData()
​
Returns the data for the available actions.
Returns ​
availableActions()
​
Returns the available actions for the current source.
Returns ​
craft\base\ElementActionInterface[], null
availableExporters()
​
- Since
- 3.4.0
Returns the available exporters for the current source.
Returns ​
craft\base\ElementExporterInterface[], null
condition()
​
- Since
- 4.0.0
Returns the condition that should be applied to the element query.
Returns ​
craft\elements\conditions\ElementConditionInterface, null
elementQuery()
​
Returns the element query based on the current params.
Returns ​
craft\elements\db\ElementQueryInterface
elementResponseData()
​
Returns the element data to be returned to the client.
Arguments ​
$includeContainer
(boolean) – Whether the element container should be included in the response data$includeActions
(boolean) – Whether info about the available actions should be included in the response data
Returns ​
exporterData()
​
- Since
- 3.4.0
Returns the data for the available exporters.
Returns ​
isAdministrative()
​
- Since
- 5.0.0
Returns whether the element index has an administrative context (index
or embedded-index
).
Returns ​
source()
​
Returns the selected source info.
Returns ​
Throws ​
- yii\web\ForbiddenHttpException
if the user is not permitted to access the requested source
viewState()
​
Returns the current view state.
Returns ​
Constants ​
Constant | Description |
---|---|
ALLOW_ANONYMOUS_LIVE | |
ALLOW_ANONYMOUS_NEVER | |
ALLOW_ANONYMOUS_OFFLINE |