-
Notifications
You must be signed in to change notification settings - Fork 173
Using block visibility
jeff-h edited this page Oct 16, 2015
·
2 revisions
Contributed by istryker and gswsdrupal:
You have a content type that you want to be display on specific pages. You want to use the same system as Drupal core block visibility. Add a textarea field to your content type called visibility, with a machine name of `field_block_visibility`.Create or add to your endpoint the following code:
/**
* Overrides parent viewEntity.
*/
public function viewEntity($id) {
global $base_path;
$request = $this->getRequest();
$entity_id = $this->getEntityIdByFieldId($id);
if (!$this->isValidEntity('view', $entity_id)) {
return;
}
$wrapper = entity_metadata_wrapper($this->entityType, $entity_id);
$wrapper->language($this->getLangCode());
// Check if there is a visibility filter.
if (isset($request['visibility'])) {
$visibility_path = $request['visibility'];
// if filter path does not match the path, then return.
// if visibility pattern is not set, then show everywhere by default.
$visibility_pattern = $wrapper->field_block_visibility->value();
// ** If you need to remove the $base_path from the beginning of path uncomment the line below **
//$visibility_path = preg_replace('/^' . str_replace('/', '\/', $base_path) . '/', '', $visibility_path);
if (isset($visibility_pattern) && !drupal_match_path($visibility_path, $visibility_pattern)) {
return;
}
}
return parent::viewEntity($id);
and to your pubicFieldsInfo()
function
$public_fields['visibility'] = array(
'property' => 'field_block_visibility',
);
Now if you go to /api/v1.0/[endpoint_name]?visiblity=[specific_path]
, only the path that will match will be returned.