Elgg\WidgetsService::createWidget PHP Method

createWidget() public method

See also: elgg_create_widget
Since: 1.9.0
public createWidget ( $owner_guid, $handler, $context, $access_id = null )
    public function createWidget($owner_guid, $handler, $context, $access_id = null)
    {
        if (empty($owner_guid) || empty($handler)) {
            return false;
        }
        $owner = get_entity($owner_guid);
        if (!$owner) {
            return false;
        }
        if (!$this->validateType($handler, $context, $owner)) {
            return false;
        }
        $widget = new \ElggWidget();
        $widget->owner_guid = $owner_guid;
        $widget->container_guid = $owner_guid;
        // @todo - will this work for group widgets?
        if (isset($access_id)) {
            $widget->access_id = $access_id;
        } else {
            $widget->access_id = get_default_access();
        }
        if (!$widget->save()) {
            return false;
        }
        // private settings cannot be set until \ElggWidget saved
        $widget->handler = $handler;
        $widget->context = $context;
        return $widget->getGUID();
    }