is API enabled?

The map example introduces a method to check for availability of a particular API, which is invoked at the beginning of the render method like this:

    public function render(Page $pg) {
        $apiEnabled = $this->ctx->isApiEnabled('MapBox');    // We need opt-in from the user
        if($apiEnabled !== true) return($apiEnabled);        // isApiEnabled returns markup in case the API is not enabled

While we seem to invoke a method from the renderer, this, in fact, ends in a call to hookable FieldtypeRepeaterFlex::isApiEnabled. This method simply returns a string and looks like that:

    public static function ___isApiEnabled($api)
        {
        return('<p>' . __('Use hook on FieldtypeRepeaterFlex::isApiEnabled to support this API: ') . $api . '<p>');
        }

To enable a particular api, which is identified by the string in $api ('MapBox' in our example), we'll need to place a hook on that method. If you never used hooks in ProcessWire, please check out Using hooks in ProcessWire first.

In this case we need a before hook which replaces the original method (by setting $event->replace = true;), something like this (placed in /site/render.php):

<?php namespace ProcessWire;

wire()->addHookBefore('FieldtypeRepeaterFlex::isApiEnabled', function($event) {
    $apiType = $event->arguments(0);   // Get API parameter
    if($apiType == 'MapBox')   // Check for correct API
        {
        if(CheckIfThisApiIsEnabled)   // This needs to be implemented!
            $event->return = true;
        else
            {
            $event->return = "This API is not enabled!";
            }
        $event->replace = true;
        return;
        }
    });

That hook should only process known API strings and simply return for anything unknown. But, hey, this is completely in your hands!

For the MapBox example the else part creates markup for a dialog. I'm using UiKit here, but you may build a dialog or whatever seems suitable the way you prefer.

            $page = wire('page');
            $Color = '#3B3';    // Button Color
            $Style = " style='background-color:{$Color};'";
            $dlgtitle = '<i class="fa fa-map-o"></i> '.__('Enable Map');    //  Activate Map
            
            // Some strings
            $dlgBodytext1 = __('The map functions use services from external servers, please notice additional information in our still-to-be-written Policy Statement.');
            $dlgBodytext2 = __('Enable Map functions now?');
            $dlgCloseButton = __('No, abort');
            $dlgCloseCheckbox = __('Close');
            $dlgOnceButton = __('Yes, once');
            $dlgWeekButton = __('Yes, one week');
            
            // Build a UiKit modal along with open button
            $event->return = "
<button class='uk-button uk-button-default uk-margin-small-right'{$Style} type='button' uk-toggle='target: #modal{$page->id}'>{$dlgtitle}</button>
<div id='modal{$page->id}' uk-modal>
 <div class='uk-modal-dialog uk-modal-body'>
  <h2 class='uk-modal-title'>{$dlgtitle}</h2>
  {$dlgBodytext1}
  <hr/>
  {$dlgBodytext2}
  <p class='uk-text-right'>
   <button class='uk-button uk-button-default uk-modal-close' type='button'>{$dlgCloseButton}</button>
   <button class='uk-modal-close-default' type='button' uk-close>{$dlgCloseCheckbox}</button>
   <a class='uk-button uk-button-primary' type='button' href='?showmap=2'>{$dlgWeekButton}</a>
   <a class='uk-button uk-button-primary' type='button' href='?showmap=1'>{$dlgOnceButton}</a>
  </p>
 </div>
</div>
";

That dialog provides two options to enable this api once or for a week, they simply add a parameter ?showmap= to the current url and re-opens the same page.

To enable the "once" functionality, just replace the if with

    if((int)wire('input')->get('showmap') == 1)

To implement the "week" option we'll set a cookie on client side. You may need to opt-in for cookies first to conform to regulations, but that's, again, another story. The actual "if" condition looks like this:

        $cookieName = 'enable-map';
        if(wire('input')->cookie($cookieName))    // Cookie exists and not expired?
            $ShowMap = 1;  // Everything fine, don't ask
        else
            {
            $ShowMap = (int)wire('input')->get('showmap');
            if($ShowMap == 2)
                {    // Request to set a cookie
                $cookieExpire = 60*60*24*7;    // One Week
                $cookieDomain = null;
                $cookieOnlySSL = false;
                $cookieHttp = true;
                setcookie($cookieName, 1, time() + $cookieExpire, '/', $cookieDomain, $cookieSSL, $cookieHttp);
                }
            else if($ShowMap != 1)
                $ShowMap = 0;
            }

        if($ShowMap)
            $event->return = true;    // API is enabled
        else
            {
            }

 

prepared in 45ms (content 14ms, header 0ms, Menu 19ms, Footer 11ms)