Manipulate form

assuncao

Member
I have been modifying many forms directly. It had a cost: I can't get update it easily anymore. So I'm looking forward to make all my customization using extensions from now. I have already one for menu itens. I got an example here and I adapted to my case and it works pretty fine.

I would like to know if can anyone give me any example how to hide an element or an entire row using extension.

Example: I need to hide "Subscriber actions" from customer/lists/create. Is it possible? Thx again!
 
@Erik Figueiredo - You could do that from CSS, it's the easiest way. Add a file called style-custom.css in /customers/assets/css and place your code there ;)

You can do it from extensions too, but will be a bit more complex, though, let me give you an examaple.
Each view file from apps/{customer,backend,frontend}/views/* area has some code like:
PHP:
<?php
}
/**
* This hook gives a chance to prepend content before the active form or to replace the default active form entirely.
* Please note that from inside the action callback you can access all the controller view variables
* via {@CAttributeCollection $collection->controller->data}
* In case the form is replaced, make sure to set {@CAttributeCollection $collection->renderForm} to false
* in order to stop rendering the default content.
* @since 1.3.3.1
*/
$hooks->doAction('before_active_form', $collection = new CAttributeCollection(array(
    'controller'    => $this,
    'renderForm'    => true,
)));

// and render if allowed
if ($collection->renderForm) {
    $form = $this->beginWidget('CActiveForm');
    ?>
[...] the rest of the page here
The important part here is this bit:
PHP:
if ($collection->renderForm) {
What this tells us, is that if the "renderForm" variable defined above:
PHP:
$hooks->doAction('before_active_form', $collection = new CAttributeCollection(array(
    'controller'    => $this,
    'renderForm'    => true,
)));
is set to false, then mailwizz will not render it's form. Keep this in mind.

We can also see that this is an action hook, which means we can simply echo content in the callbacks when we connect to it, so let's see how we can hook into it:
PHP:
<?php
Yii::app()->hooks->addAction('before_active_form', function($collection){
    // we need to make sure we are on the right controller and action so we don't do this on other pages:
   if ($collection->controller->id != 'lists' || !in_array($collection->controller->action->id, array('create', 'update'))) {
         return;
    }

    // tell mailwizz to not render it's form, because we will render ours, which is a custom one:
    $collection->renderForm = false; 

    // now we can simply show our form, but first we need to make all the variables available to us.
    extract($collection->controller->data, EXTR_SKIP);

    // now we can simply show our form, we can copy it from mailwizz and adjust it for our needs:
    $form = $this->beginWidget('CActiveForm');
    ?>
 
<div class="col-lg-6">
    <div class="form-group">
        <?php echo $form->labelEx($list, 'name');?>
        <?php echo $form->textField($list, 'name', $list->getHtmlOptions('name')); ?>
        <?php echo $form->error($list, 'name');?>
    </div>
</div>
   

<?php
  $this->endWidget();
 
});

Now, i get this might get overwhelming, but keep in mind that mailwizz's actions and filters are inspired from Wordpress, so everything applies.
If you read
http://docs.presscustomizr.com/arti...-filters-and-hooks-a-guide-for-non-developers
https://code.tutsplus.com/articles/wordpress-actions-and-filters-whats-the-difference--cms-25700
https://codex.wordpress.org/Plugin_API
http://codex.wordpress.org/Glossary#Action
http://codex.wordpress.org/Glossary#Filter

Things will start to make sense.
 
Thank you so much, @twisted1919 !!! Now it really makes sense. Actually I was almost in a right way. Look what I have done before read your reply:
PHP:
<?php
public function run()
    {
        //Altera a página de criação e edição de listas
        Yii::app()->hooks->addAction('after_active_form', function($void){
            if (Yii::app()->controller->uniqueid == 'lists'){ ?>
                <script>
                    $(document).ready(function(){
                        $("#ListCustomerNotification_subscribe").parents().eq(7).hide();
                        $("#tab-subscriber-action-when-subscribe").parents().eq(4).hide();
                        $("#ListCompany_name").parents().eq(4).hide();
                        $("#Lists_description").parents().eq(2).hide();
                        $("#Lists_display_name").parents().eq(1).hide();
                        $("#Lists_opt_in").parents().eq(2).hide();
                        $("#Lists_subscriber_404_redirect").parents().eq(2).hide();
                        $("hr:eq(0),hr:eq(1),hr:eq(2)").hide();
                        $("#ListDefault_from_name").parents().eq(2).hide();
                        $("#ListDefault_subject").parents().eq(2).hide();
                        $("#ListDefault_from_email").parents().eq(2).hide();
                        <?php
                            $command = Yii::app()->db->createCommand('SELECT remetente
                                                                        FROM {{customer}}
                                                                        WHERE customer_id = '.Yii::app()->customer->getId());
                            $results = $command->queryAll();
                            foreach ($results as $result) {
                                if ($result['remetente']!=""){
                                    echo '$("#ListDefault_reply_to").val("'.$result['remetente'].'");';
                                }
                             
                            }
                        ?>
                        $('#Lists_name').change(function(){
                            $("#Lists_description").val($("#Lists_name").val());
                         
                        });
                    });
                </script>
            <?php
            }
        });
}
My next change would be add filters to IDs, so I will start doing it in the right way as your example.
 
Yup, please remember that in case you need filters that the app don't have yet, you can ask and we can try to add them for future releases ;)
 
Back
Top